为什么iBatis+Oracle的executeBatch总是返回0?
2016-05-20 11:57
417 查看
iBatis+Oracle,调用executeBatch总是返回0,而不是真实的受影响记录数。查看代码:
注意粗体部分!rowCounts[j] == Statement.SUCCESS_NO_INFO
Oracle手册有说明 “For a prepared statement batch, it is not possible to know the number of rows affected in the database by each individual statement in the batch. Therefore, all array elements have a value of -2. According to the JDBC 2.0 specification, a value of -2 indicates that the operation was successful but the number of rows affected is unknown.”,大意是说,Oracle没有办法知道batch中某语句确切影响的记录数,而JDBC 2.0规范规定,操作成功但影响行数不确定的,返回Statement.SUCCESS_NO_INFO(-2),最终totalRowCount没有累计,一直保持0。
public class SqlExecutor { private static class Batch { public int executeBatch() throws SQLException { int totalRowCount = 0; for (int i = 0, n = statementList.size(); i < n; i++) { PreparedStatement ps = (PreparedStatement) statementList.get(i); int[] rowCounts = ps.executeBatch(); for (int j = 0; j < rowCounts.length; j++) { if (rowCounts[j] == Statement.SUCCESS_NO_INFO) { // do nothing } else if (rowCounts[j] == Statement.EXECUTE_FAILED) { throw new SQLException("The batched statement at index " + j + " failed to execute."); } else { totalRowCount += rowCounts[j]; } } } return totalRowCount; }
注意粗体部分!rowCounts[j] == Statement.SUCCESS_NO_INFO
Oracle手册有说明 “For a prepared statement batch, it is not possible to know the number of rows affected in the database by each individual statement in the batch. Therefore, all array elements have a value of -2. According to the JDBC 2.0 specification, a value of -2 indicates that the operation was successful but the number of rows affected is unknown.”,大意是说,Oracle没有办法知道batch中某语句确切影响的记录数,而JDBC 2.0规范规定,操作成功但影响行数不确定的,返回Statement.SUCCESS_NO_INFO(-2),最终totalRowCount没有累计,一直保持0。
相关文章推荐
- ibatis简单实现与配置
- Mybatis与Ibatis的区别
- The error occurred while applying a result map.
- ibatis 的缓存
- ibatis 简单使用
- Mapped Statements collection does not contain
- 简单实现ibatis的物理分页
- ibatis加memcached缓存
- ibatis 高速缓存
- 最近在用ibatis,发些文章来学习下
- 学习ibatis,参照夏晰翻译的ibatis2.0学习指南
- 从IBatis2.X 移植到IBatis3.0 sqlMapConfig and sqlMap XML 配置文件升级说明
- iBATIS教程之快速入门浅析
- struts2.1.8+mybatis3.0+spring3.0(全注解)小例子
- iBatis2--SqlMap的配置总结
- ibatis 动态Sql
- Hibernate和ibatis在同一个webapp中混合使用在spring中的配置方法
- How to use custom type handler (typeHandler) in ibatis 2.3.4
- iBatis 配置文件详解
- ibatis中#和$用法区别