Skip to content

Commit 49549d6

Browse files
author
Thomas Risberg
committedMar 12, 2009
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)
1 parent 943e359 commit 49549d6

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

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

+11
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ protected DataAccessException doTranslate(String task, String sql, SQLException
188188
return dex;
189189
}
190190

191+
// Next, try the custom SQLException translator, if available.
192+
if (this.sqlErrorCodes != null) {
193+
SQLExceptionTranslator customTranslator = this.sqlErrorCodes.getCustomSqlExceptionTranslator();
194+
if (customTranslator != null) {
195+
DataAccessException customDex = customTranslator.translate(task, sql, sqlEx);
196+
if (customDex != null) {
197+
return customDex;
198+
}
199+
}
200+
}
201+
191202
// Check SQLErrorCodes with corresponding error code, if available.
192203
if (this.sqlErrorCodes != null) {
193204
String errorCode = null;

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

+24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.jdbc.support;
1818

1919
import org.springframework.util.StringUtils;
20+
import org.springframework.util.Assert;
21+
import org.springframework.dao.InvalidDataAccessResourceUsageException;
2022

2123
/**
2224
* JavaBean for holding JDBC error codes for a particular database.
@@ -37,6 +39,8 @@ public class SQLErrorCodes {
3739

3840
private boolean useSqlStateForTranslation = false;
3941

42+
private SQLExceptionTranslator customSqlExceptionTranslator = null;
43+
4044
private String[] badSqlGrammarCodes = new String[0];
4145

4246
private String[] invalidResultSetAccessCodes = new String[0];
@@ -97,6 +101,26 @@ public boolean isUseSqlStateForTranslation() {
97101
return this.useSqlStateForTranslation;
98102
}
99103

104+
public SQLExceptionTranslator getCustomSqlExceptionTranslator() {
105+
return customSqlExceptionTranslator;
106+
}
107+
108+
public void setCustomSqlExceptionTranslatorClass(Class customSqlExceptionTranslatorClass) {
109+
if (customSqlExceptionTranslatorClass != null) {
110+
try {
111+
this.customSqlExceptionTranslator =
112+
(SQLExceptionTranslator) customSqlExceptionTranslatorClass.newInstance();
113+
}
114+
catch (InstantiationException e) {
115+
throw new InvalidDataAccessResourceUsageException(
116+
"Unable to instantiate " + customSqlExceptionTranslatorClass.getName(), e);
117+
}
118+
catch (IllegalAccessException e) {
119+
throw new InvalidDataAccessResourceUsageException(
120+
"Unable to instantiate " + customSqlExceptionTranslatorClass.getName(), e);
121+
}
122+
}
123+
}
100124

101125
public void setBadSqlGrammarCodes(String[] badSqlGrammarCodes) {
102126
this.badSqlGrammarCodes = StringUtils.sortStringArray(badSqlGrammarCodes);

0 commit comments

Comments
 (0)