Du får inte BatchUpdateException , eftersom du kanske använder SQLErrorCodeSQLExceptionTranslator
i jdbcTemplate , som hanterar BatchUpdateException s på ett speciellt sätt
:
if (sqlEx instanceof BatchUpdateException && sqlEx.getNextException() != null) {
SQLException nestedSqlEx = sqlEx.getNextException();
if (nestedSqlEx.getErrorCode() > 0 || nestedSqlEx.getSQLState() != null) {
sqlEx = nestedSqlEx;
}
}
Det finns ett problem om det:
Du kan mildra detta om du använder SQLStateSQLExceptionTranslator
:
jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
Då får du BatchUpdateException som en cause :
try {
// ...
} catch (DataAccessException e) {
Throwable cause = e.getCause();
logger.info("cause instanceof BatchUpdateException = {}", cause instanceof BatchUpdateException);
}
Men Observera att vid postgresql jdbc-drivrutin BatchUpdateException#getUpdateCounts() kommer att innehålla EXECUTE_FAILED
endast trots att en rad kunde infogas framgångsrikt.
Se detta problem