I am using batch insert from my Java application into mysql database for bulk data loading. For compiling a result of failed executions, we are handling BatchUpdateException in the following way:
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());
}
I was running a case where all executions would fail (table does not exist). So ideally I would be having a fail reason added for every record.
However, I could find that all but the first record, were being added. When I debugged the code, I found that codes[0] is coming as '-1', whereas all other are coming as '-3' (which is Statement.EXECUTE_FAILED).
As per 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')
Question: Is it that MySql connector/J does not set the update count correctly for the first execution failure in a batch, or I am missing something here? Has anyone come across any such scenario?
I am using connector/J 5.1.30; Mysql 5.5.24 on Ubuntu 12.04