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
logger
Constructor and Description |
---|
SQLErrorCodeSQLExceptionTranslator()
Constructor for use as a JavaBean.
|
SQLErrorCodeSQLExceptionTranslator(DataSource dataSource)
Create a SQL error code translator for the given DataSource.
|
SQLErrorCodeSQLExceptionTranslator(SQLErrorCodes sec)
Create a SQLErrorCode translator given these error codes.
|
SQLErrorCodeSQLExceptionTranslator(String dbName)
Create a SQL error code translator for the given database product name.
|
Modifier and Type | Method and Description |
---|---|
protected DataAccessException |
createCustomException(String task,
String sql,
SQLException sqlEx,
Class<?> exceptionClass)
Create a custom
DataAccessException , based on a given exception
class from a CustomSQLErrorCodesTranslation definition. |
protected DataAccessException |
customTranslate(String task,
String sql,
SQLException sqlEx)
Subclasses can override this method to attempt a custom mapping from
SQLException to DataAccessException . |
protected DataAccessException |
doTranslate(String task,
String sql,
SQLException ex)
Template method for actually translating the given exception.
|
SQLErrorCodes |
getSqlErrorCodes()
Return the error codes used by this translator.
|
void |
setDatabaseProductName(String dbName)
Set the database product name for this translator.
|
void |
setDataSource(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
public SQLErrorCodeSQLExceptionTranslator()
public SQLErrorCodeSQLExceptionTranslator(DataSource dataSource)
dataSource
- the DataSource to use to find meta-data and establish
which error codes are usableSQLErrorCodesFactory
public SQLErrorCodeSQLExceptionTranslator(String dbName)
dbName
- the database product name that identifies the error codes entrySQLErrorCodesFactory
,
DatabaseMetaData.getDatabaseProductName()
public SQLErrorCodeSQLExceptionTranslator(SQLErrorCodes sec)
sec
- error codespublic void setDataSource(DataSource dataSource)
Setting this property will cause a Connection to be obtained from the DataSource to get the meta-data.
dataSource
- the DataSource to use to find meta-data and establish
which error codes are usableSQLErrorCodesFactory.getErrorCodes(javax.sql.DataSource)
,
DatabaseMetaData.getDatabaseProductName()
public void setDatabaseProductName(String dbName)
Setting this property will avoid obtaining a Connection from the DataSource to get the meta-data.
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(String task, String 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
- the SQL query or update that caused the problem (if known)ex
- the offending SQLException
SQLException
;
or null
if no exception match foundprotected DataAccessException customTranslate(String task, String sql, SQLException sqlEx)
SQLException
to DataAccessException
.task
- readable text describing the task being attemptedsql
- the SQL query or update that caused the problem (may be null
)sqlEx
- the offending SQLExceptionnull
if no custom translation applies, otherwise a DataAccessException
resulting from custom translation. This exception should include the sqlEx
parameter
as a nested root cause. This implementation always returns null
, meaning that the
translator always falls back to the default error codes.protected DataAccessException createCustomException(String task, String sql, SQLException sqlEx, Class<?> exceptionClass)
DataAccessException
, based on a given exception
class from a CustomSQLErrorCodesTranslation
definition.task
- readable text describing the task being attemptedsql
- the 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
definitionnull
if the custom exception could not be created, otherwise
the resulting DataAccessException
. This exception should include the
sqlEx
parameter as a nested root cause.CustomSQLErrorCodesTranslation.setExceptionClass(java.lang.Class<?>)