public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExceptionTranslator
SQLExceptionTranslator
that analyzes vendor-specific error codes.
More precise than an implementation based on SQL state, but heavily vendor-specific.
This class applies the following matching rules:
SQLStateSQLExceptionTranslator
is the
default fallback translator, analyzing the exception's SQL state only. On Java 6
which introduces its own SQLException
subclass hierarchy, we will
use SQLExceptionSubclassTranslator
by default, which in turns falls back
to Spring's own SQL state translation when not encountering specific subclasses.
The configuration file named "sql-error-codes.xml" is by default read from this package. It can be overridden through a file of the same name in the root of the class path (e.g. in the "/WEB-INF/classes" directory), as long as the Spring JDBC package is loaded from the same ClassLoader.
SQLErrorCodesFactory
,
SQLStateSQLExceptionTranslator
Modifier and Type | Field and Description |
---|---|
private static int |
MESSAGE_ONLY_CONSTRUCTOR |
private static int |
MESSAGE_SQL_SQLEX_CONSTRUCTOR |
private static int |
MESSAGE_SQL_THROWABLE_CONSTRUCTOR |
private static int |
MESSAGE_SQLEX_CONSTRUCTOR |
private static int |
MESSAGE_THROWABLE_CONSTRUCTOR |
private SQLErrorCodes |
sqlErrorCodes
Error codes used by this translator
|
logger
Constructor and Description |
---|
SQLErrorCodeSQLExceptionTranslator()
Constructor for use as a JavaBean.
|
SQLErrorCodeSQLExceptionTranslator(javax.sql.DataSource dataSource)
Create a SQL error code translator for the given DataSource.
|
SQLErrorCodeSQLExceptionTranslator(SQLErrorCodes sec)
Create a SQLErrorCode translator given these error codes.
|
SQLErrorCodeSQLExceptionTranslator(java.lang.String dbName)
Create a SQL error code translator for the given database product name.
|
Modifier and Type | Method and Description |
---|---|
protected DataAccessException |
createCustomException(java.lang.String task,
java.lang.String sql,
java.sql.SQLException sqlEx,
java.lang.Class exceptionClass)
Create a custom DataAccessException, based on a given exception
class from a CustomSQLErrorCodesTranslation definition.
|
protected DataAccessException |
customTranslate(java.lang.String task,
java.lang.String sql,
java.sql.SQLException sqlEx)
Subclasses can override this method to attempt a custom mapping from SQLException
to DataAccessException.
|
protected DataAccessException |
doTranslate(java.lang.String task,
java.lang.String sql,
java.sql.SQLException ex)
Template method for actually translating the given exception.
|
SQLErrorCodes |
getSqlErrorCodes()
Return the error codes used by this translator.
|
private void |
logTranslation(java.lang.String task,
java.lang.String sql,
java.sql.SQLException sqlEx,
boolean custom) |
void |
setDatabaseProductName(java.lang.String dbName)
Set the database product name for this translator.
|
void |
setDataSource(javax.sql.DataSource dataSource)
Set the DataSource for this translator.
|
void |
setSqlErrorCodes(SQLErrorCodes sec)
Set custom error codes to be used for translation.
|
buildMessage, getFallbackTranslator, setFallbackTranslator, translate
private static final int MESSAGE_ONLY_CONSTRUCTOR
private static final int MESSAGE_THROWABLE_CONSTRUCTOR
private static final int MESSAGE_SQLEX_CONSTRUCTOR
private static final int MESSAGE_SQL_THROWABLE_CONSTRUCTOR
private static final int MESSAGE_SQL_SQLEX_CONSTRUCTOR
private SQLErrorCodes sqlErrorCodes
public SQLErrorCodeSQLExceptionTranslator()
public SQLErrorCodeSQLExceptionTranslator(javax.sql.DataSource dataSource)
dataSource
- DataSource to use to find metadata and establish
which error codes are usableSQLErrorCodesFactory
public SQLErrorCodeSQLExceptionTranslator(java.lang.String dbName)
dbName
- the database product name that identifies the error codes entrySQLErrorCodesFactory
,
DatabaseMetaData.getDatabaseProductName()
public SQLErrorCodeSQLExceptionTranslator(SQLErrorCodes sec)
sec
- error codespublic void setDataSource(javax.sql.DataSource dataSource)
Setting this property will cause a Connection to be obtained from the DataSource to get the metadata.
dataSource
- DataSource to use to find metadata and establish
which error codes are usableSQLErrorCodesFactory.getErrorCodes(javax.sql.DataSource)
,
DatabaseMetaData.getDatabaseProductName()
public void setDatabaseProductName(java.lang.String dbName)
Setting this property will avoid obtaining a Connection from the DataSource to get the metadata.
dbName
- the database product name that identifies the error codes entrySQLErrorCodesFactory.getErrorCodes(String)
,
DatabaseMetaData.getDatabaseProductName()
public void setSqlErrorCodes(SQLErrorCodes sec)
sec
- custom error codes to usepublic SQLErrorCodes getSqlErrorCodes()
setDataSource(javax.sql.DataSource)
protected DataAccessException doTranslate(java.lang.String task, java.lang.String sql, java.sql.SQLException ex)
AbstractFallbackSQLExceptionTranslator
The passed-in arguments will have been pre-checked. Furthermore, this method
is allowed to return null
to indicate that no exception match has
been found and that fallback translation should kick in.
doTranslate
in class AbstractFallbackSQLExceptionTranslator
task
- readable text describing the task being attemptedsql
- SQL query or update that caused the problem (may be null
)ex
- the offending SQLException
SQLException
;
or null
if no exception match foundprotected DataAccessException customTranslate(java.lang.String task, java.lang.String sql, java.sql.SQLException sqlEx)
task
- readable text describing the task being attemptedsql
- SQL query or update that caused the problem. May be null
.sqlEx
- the offending SQLExceptionprotected DataAccessException createCustomException(java.lang.String task, java.lang.String sql, java.sql.SQLException sqlEx, java.lang.Class exceptionClass)
task
- readable text describing the task being attemptedsql
- SQL query or update that caused the problem. May be null
.sqlEx
- the offending SQLExceptionexceptionClass
- the exception class to use, as defined in the
CustomSQLErrorCodesTranslation definitionCustomSQLErrorCodesTranslation.setExceptionClass(java.lang.Class)
private void logTranslation(java.lang.String task, java.lang.String sql, java.sql.SQLException sqlEx, boolean custom)