我正在使用批量插入从我的Java应用程序到mysql数据库进行批量数据加载。为了编译执行失败的结果,我们通过以下方式处理BatchUpdateException:
catch (BatchUpdateException e)
{
int[] codes = e.getUpdateCounts();
for (int i = 0; i<codes.length; i++)
{
if (Statement.EXECUTE_FAILED == codes[i])
{
lr.recordLoadCount--;
//`records` is a List containing the objects which were
//added in this batch
lr.addFailReason(records.get(i).xmlString());
}
}
lr.addException(e.getMessage());
}
我正在运行一个所有执行都会失败的案例(表不存在)。所以理想情况下,我将为每条记录添加一个失败原因。
但是,我可以发现除了第一条记录之外的所有记录都被添加了。当我调试代码时,我发现代码[0]以“-1”的形式出现,而所有其他代码都以“-3”的形式出现(即语句。EXECUTE_FAILED)。
根据Javadocs:
public int[] getUpdateCounts() Returns: an array of int containing the update counts for the updates that were executed successfully before this error occurred. Or, if the driver continues to process commands after an error, one of the following for every command in the batch: - an update count - Statement.SUCCESS_NO_INFO ('-2') - Statement.EXECUTE_FAILED ('-3')
问:是MySql连接器/J没有正确设置批处理中第一次执行失败的更新计数,还是我在这里遗漏了一些东西?有人遇到过这种情况吗?
我正在使用连接器/J 5.1.30;Ubuntu 12.04上的Mysql 5.5.24
我发现问题是“rewrite eBatched语句=true”连接选项。当使用此选项时,会发生报告的问题。将其报告为bug。