Verifying success for spring JDBC batch update

Ayushi picture Ayushi · Oct 7, 2013 · Viewed 11k times · Source

I am using Spring JDBCTemplate batchUpdate to insert data in a batch. I want to verify if the data is successfully inserted. JDBCTemplate batchUpdate returns an int[][], so which is the right way to verify that the data is inserted?

This link says "All batch update methods return an int array containing the number of affected rows for each batch entry. This count is reported by the JDBC driver and it's not always available in which case the JDBC driver simply returns a -2 value". I am unable to understand the significance of returning -2 value here. Does it means the insert was unsuccessful?

Answer

aviad picture aviad · Oct 7, 2013

-2 does not necessarily mean error, it might be as mentioned, the case of count of affected rows is not available.

EDIT

-2 is the value of Statement.SUCCESS_NO_INFO (while EXECUTE_FAILED is -3). So unless the driver does not comply with the JDBC specification, -2 unequivocally means success

END OF EDIT

The errors are reported via BatchUpdateException

Normally, if you run N queries in your batch script you will get the count of updates per query i in the result:

int result[] = jdbcTemplate.batchUpdate(sql);

so:

result[0]

will hold the update count for the first query,

result[1]

will hold the update count for the second query etc.