最近考虑到优化数据导入这块,所以看看通过batch这块内容有没有改进的余地。
但是加入startBatch(),executeBatch(),感觉没有什么效果。
下面是我测试时候的概略情况:
MastSvr这个Service方法已注册,同时已注入SqlMapDao,dao是接口
MastSvr.addData(){
//获取goodsList 商品列表, bankList 部门列表
.......
//对每个商品id和部门id分别插入一条 记录到Mast表中
dao.insertBuNaTaxBatLst(goodsList, bankList);
}
SqlMapDao中:
public void insertBuNaTaxBatLst( final List goodsList, final List bankList {
getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
executor.startBatch();
// getSqlMapClientTemplate().getSqlMapClient().getDataSource().getConnection().setAutoCommit( true );
System.out.println("当前事物状态: " + getSqlMapClientTemplate().getSqlMapClient().getDataSource().getConnection().getAutoCommit() );
// do some iBatis operations here
for(int j= 0 ;j< goodsList.size();j++){
OlyGoods olyGoods = (OlyGoods)goodsList.get(j);
Mast mast = new Mast();
mast.setGoodsId(olyGoods.getGoodsId());
for (int i = 0; i < bankList.size(); i++) {
mast.setDeptId( ""+((Bank) bankList.get(i)).getBankid() );
mast.setInNum(new Long(0));
mast.setOraNum(new Long(0));
mast.setOutNum(new Long(0));
mast.setRepayAmt(new BigDecimal(0));
mast.setReturnNum(new Long(0));
mast.setSaleNum(new Long(0));
mast.setStockNum(new Long(0));
mast.setSubReturnNum(new Long(0));
mast.setWaitInNum(new Long(0));
insertBySql( "OLY_MAST_GOODS.addMastGoods", mast);
}
}
executor.executeBatch();
return null;
}
});
}
junit测试结果是:
6248记录,Batch 加上和不加上差不多,基本都是18s左右。
问题1:查询网上文章,说JDBC的AutoCommit属性默认被设置为true的,但是我打印出来为false(而且设置没用,我用getSqlMapClientTemplate().getSqlMapClient().getDataSource().getConnection().setAutoCommit( true );或配置文件里加
<property name="defaultAutoCommit"><value>true</value></property> 这些方法也没有用,始终是false),但如果把addData()方法改名,让其得不到事务处理,打印出来就是true,设置就有用了,是不是事务处理时自动将AutoCommit设置为false了?
问题2:如何真正发挥batch的作用