U krijgt geen BatchUpdateException , omdat je misschien SQLErrorCodeSQLExceptionTranslator
in jdbcTemplate , die BatchUpdateException afhandelt s op een speciale manier
:
if (sqlEx instanceof BatchUpdateException && sqlEx.getNextException() != null) {
SQLException nestedSqlEx = sqlEx.getNextException();
if (nestedSqlEx.getErrorCode() > 0 || nestedSqlEx.getSQLState() != null) {
sqlEx = nestedSqlEx;
}
}
Er is een probleem mee:
U kunt dit verminderen als u de SQLStateSQLExceptionTranslator
:
jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
Dan krijgt u de BatchUpdateException als een cause :
try {
// ...
} catch (DataAccessException e) {
Throwable cause = e.getCause();
logger.info("cause instanceof BatchUpdateException = {}", cause instanceof BatchUpdateException);
}
Maar merk op dat in het geval van postgresql jdbc-stuurprogramma BatchUpdateException#getUpdateCounts() bevat EXECUTE_FAILED
alleen, ondanks het feit dat een rij met succes kon worden ingevoegd.
Zie dit probleem