简体   繁体   中英

How to write a transaction in DAO class of Spring ibatis

How to write a transaction in Spring Ibatis DAO implementation class.

I am using following code for batch update.But it is taking more than 10seconds to update 200 records.When i searched in google i found that it will work faster if we implement the transaction in this batch update.

My batch update code is like this (in the class 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();
    }

}

when i used getSqlMapClient().startTransaction() in this function it is showing an error like below

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

The second error occured because there is no transaction manager for ibatis. You can configure as the following.

<!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"/> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM