【问题标题】:How to write a transaction in DAO class of Spring ibatis如何在Spring ibatis的DAO类中编写事务
【发布时间】:2012-12-05 11:42:18
【问题描述】:

如何在Spring Ibatis DAO实现类中编写事务。

我正在使用以下代码进行批量更新。但是更新200条记录需要10多秒。当我在google中搜索时,我发现如果我们在此批量更新中实现事务会更快。

我的批量更新代码是这样的(在SensorValueLastBeanDAOImpl类中)

public int processBatchUpdate(
        final List<SensorValueLastBean> sensorValueLast) {

    Integer updateCount = 0;
    try {


        updateCount = (Integer) getSqlMapClientTemplate().execute(
                new SqlMapClientCallback<Object>() {
                    public Object doInSqlMapClient(SqlMapExecutor executor)
                            throws SQLException {                                           
                        executor.startBatch();
                        Iterator<SensorValueLastBean> iter = sensorValueLast
                                .iterator();
                        while (iter.hasNext()) {
                            executor.update(
                                    "sensor_values_last.ibatorgenerated_updateByPrimaryKeySelective",
                                    iter.next());
                        }
                                                                                    executor.executeBatch();
                        return new Integer(executor.executeBatch());

                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }

}

当我在此函数中使用 getSqlMapClient().startTransaction() 时,它显示如下错误

java.lang.NullPointerException 在 com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.startTransaction(SqlMapExecutorDelegate.java:684)
在 com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.startTransaction(SqlMapSessionImpl.java:164)
在 com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.startTransaction(SqlMapClientImpl.java:140)

【问题讨论】:

    标签: java mysql performance ibatis


    【解决方案1】:

    出现第二个错误是因为没有ibatis的事务管理器。可以如下配置。

    <!DOCTYPE sqlMapConfig
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" 
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
    
    <sqlMapConfig>
      <settings useStatementNamespaces="true"/>
        <transactionManager type="JDBC">
          <dataSource type="SIMPLE">
          <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
          <property name="JDBC.ConnectionURL"
          value="jdbc:mysql://localhost:3306/mydb"/>
          <property name="JDBC.Username" value="root"/>
          <property name="JDBC.Password" value="root"/>
        </dataSource>
     </transactionManager>
    <sqlMap resource="Contact.xml"/> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      相关资源
      最近更新 更多