您的位置:首页 > 数据库 > Oracle

为什么iBatis+Oracle的executeBatch总是返回0?

2016-05-20 11:57 417 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ibatis