Browse Source

added the option of providing a database specific custom SQLExceptionTranslator to provide customized translation for any SQLException before the error codes translation happens (SPR-4899)

conversation
Thomas Risberg 16 years ago
parent
commit
49549d66ae
  1. 11
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java
  2. 24
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodes.java

11
org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java

@ -188,6 +188,17 @@ public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExcep @@ -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;

24
org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodes.java

@ -17,6 +17,8 @@ @@ -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 { @@ -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 { @@ -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);

Loading…
Cancel
Save