【发布时间】: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