- 浏览: 921717 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (251)
- WebService (17)
- IBatis (22)
- Hibernate (1)
- SpringMVC - 基础篇 (32)
- Spring (15)
- Java (11)
- JVM及调优 - 基础篇 (4)
- 集群 (14)
- 数据库 (17)
- WebSphere (5)
- 多线程 (4)
- 集合、容器 (2)
- DB Pool (1)
- Power Designer (5)
- Maven基础 (5)
- JS (14)
- WEB 前端 (5)
- 实用小工具 (17)
- 社会、人 (2)
- 乱七八糟 (18)
- ASM&CGLIB - 基础篇 (12)
- 缓存 (1)
- 性能 (1)
- 设计之殇 (1)
- 分布式事务 (1)
- 单点登录 (11)
- 分布式 Session (4)
- Memcached - 基础篇 (6)
最新评论
-
一笑_奈何:
楼主写的还真行不错。
扫盲贴 - J2EE集群之JNDI集群实现 -
xuezhongyu01:
博主写的很详细,但最后还是没明白,最后调用BasicDataS ...
Spring中的destroy-method方法 -
Mr梁:
commons-fileupload.jar commons- ...
SpringMVC 中文件上传 MultipartResolver -
Eywa:
总结的很不错
ORACLE CASE WHEN 及 SELECT CASE WHEN的用法 -
TryRelax:
fastjson 比 jackson 好用吧?
Spring MVC Jackson DateFormat
spring集成了ibatis的批量提交的功能,我们只要调用API就可以了
首先在你的dao中需要继承org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
然后在代码中调用getSqlMapClientTemplate方法, 覆写SqlMapClientCallback类中的doInSqlMapClient的方法
public void insertTreeCateBatch(final List<Customer> customerList) throws DataAccessException{
this.getSqlMapClientTemplate().execute(new SqlMapClientCallback(){
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
executor.startBatch();
int batch = 0;
for(Customercustomer:customerList){
executor.insert("Customer.insertCustomer", TreeCate);
batch++;
//每500条批量提交一次。
if(batch==500){
executor.executeBatch();
batch = 0;
}
}
executor.executeBatch();
return null;
}
});
}
转载:http://san-yun.iteye.com/blog/900949
本文重点:
1. 在执行批处理时,方法update和executeBatch的返回值(影响的记录数)不可靠。
2. 批处理必须在显式的事务中进行,并且关闭auto commit。
3. Batch大小。
一.JDBC批处理
和CRUD一样,iBatis通过JDBC支持,封装实现了自己的批处理。下面是一段使用JDBC进行批处理的代码:
Connection conn = ...; conn.setAutoCommit(false); PreparedStatement ps = conn.prepareStatement("insert into tb (id) values (?)"); for (int i = 0; i < 1000; i++) { ps.setInt(1, i); ps.addBatch(); } ps.executeBatch(); conn.commit(); ps.close(); // Move this line into a finally block. conn.close();
JDBC 3.0规范提到, JDBC驱动会在调用PreparedStatement#executeBatch时做commit。需要小心的是批处理失败的情况:如果关闭auto commit,当发生错误时,可以调用rollback进行回滚,也可以调用commit提交成功执行的那部分修改;但如果打开auto commit,如何处理由实现(驱动)决定。因此,在执行批处理时,应当总是关闭auto commit。
Oracle 9i特性中,给出了一些最佳实践,包括:
1. 在执行批处理时,总是关闭auto commit;
2. Batch的大小应保持在10左右;
Oracle后续的版本(10g/11g),也要求关闭auto commit。但是,在11g中,推荐的batch大小介于50到100之间。
Oracle recommends you to keep the batch sizes in the general range of 50 to 100. This is because though the drivers support larger batches, they in turn result in a large memory footprint with no corresponding increase in performance. Very large batches usually result in a decline in performance compared to smaller batches.
Oracle在批处理上还有些其它限制,具体可以参考文后的链接。我们可能不需要关注这样的细节,最好咨询DBA。
二.update和executeBatch返回值
iBatis SqlMap文档提到,批处理执行时,JDBC驱动可以不返回更新的记录数。所以在批处理中,不要依赖update、delete、updateBatch等方法的返回值。
Note that it is entirely legal for the JDBC driver to fail to return the number of records updated in a batch - in which case the executeBatch() method will return 0 even though records have been updated. The Oracle driver is a good example of a driver that behaves this way.
但是,如果一条insert语句的sqlMap定义了selectKey语句,批处理中的insert仍然会正确返回主键值,因为SelectKeyStatement是在批处理外进行的。请参考SqlMapExecutorDelegate#insert。
三.batch与事务
批处理必须总是包裹在一个显式的事务中,否则iBatis会无视batch,逐条执行。请参考SqlMapExecutorDelegate#insert。
A batch should ALWAYS be nested inside an explicit transaction. If you fail to use an explicit transaction, then iBATIS will execute each statement individually as if you had never started a batch.
在使用到批处理时,我们通常在DAO这样写:
getSqlMapClientTemplate().execute(new SqlMapClientCallback() { public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { executor.startBatch(); for (QuotationItemTemplateDO template : quotationItemTemplates) { executor.insert("MS-CREATE-QUOT-ITEM-TEMPLATE", template); } executor.executeBatch(); return null; } });
SqlMapClientTemplate会自动包裹一个UserProvidedTransaction,不过遗憾的是,这个事务不会关闭auto commit。所以我们还是需要在Biz层包裹一个Spring管理的事务。请参考DataSourceTransactionManager#doBegin,这个方法关闭了auto commit。
四.参考
² JDBC 3.0 Spec: http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/jdbc-3_0-fr-spec.pdf?BundledLineItemUUID=TBGJ_hCujZcAAAEpc8FCxpJr&OrderID=ugmJ_hCukvUAAAEpZcFCxpJr&ProductID=eKnACUFBKakAAAEYLrU5AXiq&FileName=/jdbc-3_0-fr-spec.pdf
² Oracle 9i Feature:
http://www.oracle.com/technology/products/oracle9i/daily/jun07.html
² Oracle 10g – Performance Extensions:
http://download.oracle.com/docs/cd/B19306_01/java.102/b14355/oraperf.htm#i1059053
² Oracle 11g – Performance Extensions:
http://download.oracle.com/docs/cd/E11882_01/java.112/e10589/oraperf.htm#i1056232
² iBatis SqlMap Document:
http://svn.apache.org/repos/asf/ibatis/java/ibatis-2/trunk/ibatis-2-docs/en/iBATIS-SqlMaps-2_en.pdf
评论
executor.executeBatch();
batch = 0;
}
需要修改成 if(batch==500){
executor.executeBatch();
executor.startBatch();
batch = 0;
}
否则批量提交500调之后,后面的记录还是会每条都开一个游标。
批量操作一般是更新或删除操作,它们的返回结果一般是“影响的记录数”,但是“影响的记录数”又是不可靠的,因此不建议返回影响的记录数。
发表评论
-
iBATIS缓存
2011-12-12 21:33 1122为了提高应用程序性 ... -
一个Spring+iBatis框架进行batch处理的问题
2011-09-05 23:23 1589在使用org.springframework.jdbc.dat ... -
ibatis 注意点
2011-08-08 19:28 1336insert,update,delete 返回值 inser ... -
ibatis 之 动态SQL查询(dynamic )
2011-06-12 12:13 2252映射文件: <select id=" ... -
ibatis 之 复杂类型集合的属性
2011-06-12 12:10 2115Result Map还可以装入代表复杂类型对象集合(List) ... -
ibatis 之 复杂类型属性(即自定义类型的属性)
2011-06-12 11:54 3419复杂类型用以表示在数 ... -
ibatis 之 java.util.Map作为parameterClass和resultClass
2011-06-12 11:21 51241.Map作为parameterClass 映射 ... -
ibatis Tips 之 resultMap
2011-05-29 21:04 1156转载:http://xulongfa.iteye.com/bl ... -
ibatis Tips之parameterMap
2011-05-29 21:03 1312转载:http://xulongfa.iteye.com/bl ... -
iBatis查询API
2011-05-29 12:34 1823转载:http://sarin.iteye.com/blog/ ... -
iBatis查询select详解
2011-05-29 12:20 2189转载:http://sarin.iteye.com/blog/ ... -
ibatis 表与表的关联查询
2011-05-22 15:04 1237ibatis高级特性,处理表与表之间的关联。ibatis中,提 ... -
ibatis 动态映射机制
2011-05-22 14:15 1423对于这个组合查询页 ... -
sqlMapConfig.xml配置文件详解
2011-05-22 13:04 3522sqlMapConfig.xml配置文件详解: X ... -
ibatis缓存
2011-05-22 12:52 1324iBatis的缓存配置比较简单易懂,以我使用的iBati ... -
ibatis resultclass "java.util.hashmap" 缓存问题
2011-05-22 12:13 1486在做ibatis项目过程中遇到如下样式动态查询 <se ... -
ibatis # $区别
2011-05-22 11:55 14071、#可以进行预编译,进行类型匹配,#变量名# 会转化为 j ... -
iBATIS入门示例及注释
2011-05-08 18:20 1718工程的结构: 一、SqlMapConf ... -
iBATIS 三个版本小细节对比
2011-05-08 16:12 1176iBATIS 三个版本小细节对比 sqlMapConfig ... -
深入分析 iBATIS 框架之系统架构与映射原理
2011-05-07 12:48 1186原文:http://www.ibm.com/developer ...
相关推荐
ibatis批处理.doc ibatis批处理.doc
NULL 博文链接:https://san-yun.iteye.com/blog/900949
ibatis批量操作存在两种方式: 一种是直接在代码中进行循环操作,另一种是在配置文件中进行循环操作。
ibatis 完美例子 一对多 批处理 事务 和 spring struts2集成 ,一朵多 插入1万条数据,不到2秒,备注不包含类库
基本篇重点讲述了数据批处理的核心概念、典型的作业配置、作业步配置,以及Spring Batch框架中经典的三步走策略:数据读、数据处理和数据写,详尽地介绍了如何对CVS格式文件、JSON格式文件、XML文件、数据库和JMS...
ibatis学习 ibatis总结 ibatis ibatis ibatis
ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料
技术分享:ibatis的两种方式实现批处理
ibatis demo,ibatis例子,ibatis示例
iBATIS 级联iBATIS 级联iBATIS 级联
ibatis的原码 ibatis源码 ibatis源码 ibatis源码
ibatis代码自动生成,ibatis代码自动生成,ibatis代码自动生成,ibatis代码自动生成
Ibatis3,手册,Ibatis3手册,Ibatis3使用手册,Ibatis3参考手册
Spring与iBATIS的集成 iBATIS似乎已远离众说纷纭的OR框架之列,通常人们对非常流行的Hibernate情有独钟。但正如Spring A Developer's Notebook作者Bruce Tate 和Justin Gehtland所说的那样,与其他的OR框架相比...
ibatis api,ibatis文档,ibatis说明文档
ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南
ibatis
ibatis 乱码解决 ibatis乱码 gbk iso_8859_1 乱码
《iBATIS实战》是讲述iBATIS框架的权威著作。书中既详实地介绍了iBATIS的设计理念和基础知识,也讨论了动态SQL、高速缓存、DAD框架等高级主题,还讲解了iBATIS在实际开发中的应用。《iBATIS实战》的最后给出了一个...
ibatis教程,ibatis帮助文档,ibatis学习入门