protected SQLException findSQLException(Throwable ex) { if (ex instanceof SQLException) { return (SQLException) ex; } else { Throwable cause = ex.getCause(); if (cause != null && !cause.equals(ex)) { return findSQLException(cause); } } return null; }
public boolean isUniqueKeyViolation(Throwable ex) { boolean primaryKeyViolation = false; if (primaryKeyViolationCodes != null || primaryKeyViolationSqlStates != null) { SQLException sqlEx = findSQLException(ex); if (sqlEx != null) { if (primaryKeyViolationCodes != null) { int errorCode = sqlEx.getErrorCode(); for (int primaryKeyViolationCode : primaryKeyViolationCodes) { if (primaryKeyViolationCode == errorCode) { primaryKeyViolation = true; break; } } } if (primaryKeyViolationSqlStates != null) { String sqlState = sqlEx.getSQLState(); if (sqlState != null) { for (String primaryKeyViolationSqlState : primaryKeyViolationSqlStates) { if (primaryKeyViolationSqlState != null && primaryKeyViolationSqlState.equals(sqlState)) { primaryKeyViolation = true; break; } } } } } } return primaryKeyViolation; }
public boolean isForeignKeyViolation(Throwable ex) { boolean foreignKeyViolation = false; if (foreignKeyViolationCodes != null || foreignKeyViolationSqlStates != null) { SQLException sqlEx = findSQLException(ex); if (sqlEx != null) { if (foreignKeyViolationCodes != null) { int errorCode = sqlEx.getErrorCode(); for (int foreignKeyViolationCode : foreignKeyViolationCodes) { if (foreignKeyViolationCode == errorCode) { foreignKeyViolation = true; break; } } } if (foreignKeyViolationSqlStates != null) { String sqlState = sqlEx.getSQLState(); if (sqlState != null) { for (String foreignKeyViolationSqlState : foreignKeyViolationSqlStates) { if (foreignKeyViolationSqlState != null && foreignKeyViolationSqlState.equals(sqlState)) { foreignKeyViolation = true; break; } } } } } } return foreignKeyViolation; }