我想执行以下操作:
(从提取的一行PROCESSING DB转移到一行的PRESENTATION DB)
PreparedStatement stmt;
Statement st;
Connection conn = ConnectionManager.getInstance().getConnection(ConnectionManager.PRESENTATION_DB);
try {
conn.setAutoCommit(false);
stmt = conn.prepareStatement(
"insert into customer (name,category,age) values (?,?,?)");
stmt.clearParameters();
stmt.setString(1,"name2");
stmt.setString(2,"cat2");
stmt.setInt(3,25);
stmt.addBatch();
stmt.clearParameters();
stmt.setString(1,"name3");
stmt.setString(2,"cat3");
stmt.setInt(3,25);
stmt.addBatch();
stmt.clearParameters();
stmt.setString(1,"name4");
stmt.setString(2,"cat2");
stmt.setInt(3,25);
stmt.addBatch();
stmt.clearParameters();
stmt.setString(1,"name4");
stmt.setString(2,"cat2");
//stmt.setInt(3,25);
//this is parameter with false input
stmt.setString(3,"null");
stmt.addBatch();
stmt.clearParameters();
stmt.setString(1,"name5");
stmt.setString(2,"cat5");
stmt.setInt(3,25);
stmt.addBatch();
stmt.clearParameters();
int [] updateCounts = stmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
stmt.close();
conn.close();
}
catch(BatchUpdateException b) {
System.err.println("-----BatchUpdateException-----");
System.err.println("SQLState: " + b.getSQLState());
System.err.println("Message: " + b.getMessage());
System.err.println("Vendor: " + b.getErrorCode());
System.err.print("Update counts: ");
int [] updateCounts = b.getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++) {
System.err.print(updateCounts[i] + " ");
}
System.err.println("");
}
catch (Exception e) {
e.printStackTrace();
}
如何在没有此特定行的情况下识别导致异常能够重新执行批处理的行? (通过更新行的字段hasError)。
目前我没有按批次保存对象,所以我可以标记该行,如果它导致错误并跳过该行并继续处理(即保存/传输和删除)另一行。
谢谢!
更新:
好的,我使用以下代码来跟踪行(如果有任何行导致错误):
public static void processUpdateCounts(int[] updateCounts) {
for (int i=0; i<updateCounts.length; i++) {
if (updateCounts[i] >= 0) {
// Successfully executed; the number represents number of affected rows
logger.info("Successfully executed: number of affected rows: "+updateCounts[i]);
} else if (updateCounts[i] == Statement.SUCCESS_NO_INFO) {
// Successfully executed; number of affected rows not available
logger.info("Successfully executed: number of affected rows not available: "+updateCounts[i]);
} else if (updateCounts[i] == Statement.EXECUTE_FAILED) {
logger.info("Failed to execute: "+updateCounts[i]);
// Failed to execute
}
}
}
并补充说:
processUpdateCounts(updateCounts);
所以我可以看到有或没有任何错误的变化,但似乎BatchUpdateException不起作用?
来自BatchUpdateException的javadoc的引号:
更新计数数组中元素的顺序对应于命令添加到批处理的顺序。
在批量更新中的命令无法正确执行并且抛出BatchUpdateException之后,驱动程序可能会也可能不会继续处理批处理中的其余命令。如果驱动程序在发生故障后继续处理,则BatchUpdateException.getUpdateCounts方法返回的数组将为批处理中的每个命令提供一个元素,而不是仅包含在错误之前成功执行的命令的元素。在驱动程序继续处理命令的情况下,任何失败的命令的数组元素是Statement.EXECUTE_FAILED。
正如已经指出的那样,您只能访问批处理期间失败的SQL语句的索引。这意味着为了重新创建无法执行的相同语句,您应该以可索引的方式存储参数,例如:
List<...>paramList = new ArrayList<...>();
for (... params in paramList) {
stmt.clearParameters();
stmt.setString(...something involving params...);
// etc.
}
这样,getUpdateCounts
与EXECUTE_FAILED
数组的索引可用于识别哪些参数导致失败。
创建一个有助于执行命令的数据库层,如果sql命令失败,使用while循环或其他东西重试几次,如果在几次后仍然失败然后记录它。
我正在使用MySQL的mysql-connector-java-3.1.14。本主题顶部的更新(使用BatchUpdateException和getUpdateCounts)正常工作。现在我可以在批量插入/更新中找到有问题的行。