|
因为近期牵涉到有可能大批量更新数据的操作,于是找来Ibatis看了一下其中的批量存储,发现批处理。
代码片段:
- ....
- try{
- this.startBatch();
-
- for (String str : list<String>) {
- this.update(UP_USER_STATE, str);
- }
-
- this.executeBatch();
-
- }catch(Exception es){
- backbool = false;
- throw es;
- }
- .....
startBatch(); //开始一个批处理代码段
this.pdate(UP_USER_STATE, str); //增加更新批处理
executeBatch(); //将之前开始的批处理代码段提交执行
以上代码如转为JDBC操作,可等同于如下代码
- ........
- con.setAutoCommit(false);
- Statement stmt = con.createStatement();
- for (String str : list<String>) {
- stmt.addBatch("update xxx set state=1 where name = '" + str + "'");
- }
- int[] up_count = stmt.executeBatch();
- .....
可以看出,Ibatis每一次批处理,表示得到一个新的stmt,然后通过这个stmt进行批量操作。
因此如果在执行海量数据操作时,应该将更新语句放在一个事务然后分为多次Batch提交,这样对缓存的压力以及更新效率会更高。
|