I am using Postgres 9.3, Spring and Java.
For BatchUpdateException from javadoc:
After a command in a batch update fails to execute properly and a
BatchUpdateException
is thrown, the driver may or may not continue to process the remaining commands in the batch. If the driver continues processing after a failure, the array returned by the methodBatchUpdateException.getUpdateCounts
will have an element for every command in the batch rather than only elements for the commands that executed successfully before the error. In the case where the driver continues processing commands, the array element for any command that failed isStatement.EXECUTE_FAILED
.
The I register following translator in jdbcTemplate
public class DuplicateRecordSQLErrorCodesTranslator extends SQLErrorCodeSQLExceptionTranslator {
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
String errorCode = ex.getErrorCode() == 0 ? ex.getSQLState() : String.valueOf(ex.getErrorCode());
if (ex instanceof BatchUpdateException) {
int[] updatesCount = ((BatchUpdateException) ex).getUpdateCounts();
Then in scenario I am trying to insert a batch to DB of 100 entries, but 50 of them are duplicated. Eventually I am not able to retrieve all duplicated entries in the first transaction because updateCounts
returned is always 1.