diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java index 7415b82ea1..5545511b20 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java @@ -188,6 +188,17 @@ public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExcep return dex; } + // Next, try the custom SQLException translator, if available. + if (this.sqlErrorCodes != null) { + SQLExceptionTranslator customTranslator = this.sqlErrorCodes.getCustomSqlExceptionTranslator(); + if (customTranslator != null) { + DataAccessException customDex = customTranslator.translate(task, sql, sqlEx); + if (customDex != null) { + return customDex; + } + } + } + // Check SQLErrorCodes with corresponding error code, if available. if (this.sqlErrorCodes != null) { String errorCode = null; diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodes.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodes.java index 3035982817..5c35899ad3 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodes.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodes.java @@ -17,6 +17,8 @@ package org.springframework.jdbc.support; import org.springframework.util.StringUtils; +import org.springframework.util.Assert; +import org.springframework.dao.InvalidDataAccessResourceUsageException; /** * JavaBean for holding JDBC error codes for a particular database. @@ -37,6 +39,8 @@ public class SQLErrorCodes { private boolean useSqlStateForTranslation = false; + private SQLExceptionTranslator customSqlExceptionTranslator = null; + private String[] badSqlGrammarCodes = new String[0]; private String[] invalidResultSetAccessCodes = new String[0]; @@ -97,6 +101,26 @@ public class SQLErrorCodes { return this.useSqlStateForTranslation; } + public SQLExceptionTranslator getCustomSqlExceptionTranslator() { + return customSqlExceptionTranslator; + } + + public void setCustomSqlExceptionTranslatorClass(Class customSqlExceptionTranslatorClass) { + if (customSqlExceptionTranslatorClass != null) { + try { + this.customSqlExceptionTranslator = + (SQLExceptionTranslator) customSqlExceptionTranslatorClass.newInstance(); + } + catch (InstantiationException e) { + throw new InvalidDataAccessResourceUsageException( + "Unable to instantiate " + customSqlExceptionTranslatorClass.getName(), e); + } + catch (IllegalAccessException e) { + throw new InvalidDataAccessResourceUsageException( + "Unable to instantiate " + customSqlExceptionTranslatorClass.getName(), e); + } + } + } public void setBadSqlGrammarCodes(String[] badSqlGrammarCodes) { this.badSqlGrammarCodes = StringUtils.sortStringArray(badSqlGrammarCodes);