iBatis+Oracle,调用executeBatch总是返回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。