Non ottieni BatchUpdateException , perché potresti usare SQLErrorCodeSQLExceptionTranslator
in jdbcTemplate , che gestisce BatchUpdateException s in modo speciale
:
if (sqlEx instanceof BatchUpdateException && sqlEx.getNextException() != null) {
SQLException nestedSqlEx = sqlEx.getNextException();
if (nestedSqlEx.getErrorCode() > 0 || nestedSqlEx.getSQLState() != null) {
sqlEx = nestedSqlEx;
}
}
C'è un problema al riguardo:
Puoi mitigare questo, se usi SQLStateSQLExceptionTranslator
:
jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
Quindi otterrai la BatchUpdateException come cause :
try {
// ...
} catch (DataAccessException e) {
Throwable cause = e.getCause();
logger.info("cause instanceof BatchUpdateException = {}", cause instanceof BatchUpdateException);
}
Ma nota che in caso di driver jdbc postgresql BatchUpdateException#getUpdateCounts() conterrà EXECUTE_FAILED
solo, nonostante alcune righe siano state inserite correttamente.
Vedi questo problema