简体   繁体   English

如何在Spring ibatis的DAO类中编写事务

[英]How to write a transaction in DAO class of Spring ibatis

How to write a transaction in Spring Ibatis DAO implementation class. 如何在Spring Ibatis DAO实现类中编写事务。

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. 我正在使用以下代码进行批处理更新。但是更新200条记录需要花费10秒钟以上的时间。

My batch update code is like this (in the class SensorValueLastBeanDAOImpl) 我的批处理更新代码是这样的(在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 当我在此函数中使用getSqlMapClient()。startTransaction()时,它显示如下错误

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

The second error occured because there is no transaction manager for ibatis. 发生第二个错误,因为没有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"/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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