|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
public class SQLErrorCodeSQLExceptionTranslator
Implementation of SQLExceptionTranslator that analyzes vendor-specific error codes. More precise than an implementation based on SQL state, but vendor-specific.
This class applies the following matching rules:
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).
SQLErrorCodesFactory
,
SQLStateSQLExceptionTranslator
Field Summary | |
---|---|
protected Log |
logger
Logger available to subclasses |
Constructor Summary | |
---|---|
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. |
Method Summary | |
---|---|
protected String |
buildMessage(String task,
String sql,
SQLException sqlEx)
Build a message String for the given SQLException. |
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. |
SQLExceptionTranslator |
getFallbackTranslator()
Return the fallback exception translator. |
SQLErrorCodes |
getSqlErrorCodes()
Return the error codes used by this translator. |
SQLExceptionTranslator |
getSubclassTranslator()
Return the subclass exception translator. |
void |
setDatabaseProductName(String dbName)
Set the database product name for this translator. |
void |
setDataSource(DataSource dataSource)
Set the DataSource for this translator. |
void |
setFallbackTranslator(SQLExceptionTranslator fallback)
Override the default SQL state fallback translator. |
void |
setSqlErrorCodes(SQLErrorCodes sec)
Set custom error codes to be used for translation. |
void |
setSubclassTranslator(SQLExceptionTranslator subclassTranslator)
Override the default SQLException subclass translator. |
DataAccessException |
translate(String task,
String sql,
SQLException sqlEx)
Translate the given SQLException into a generic DataAccessException . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final Log logger
Constructor Detail |
---|
public SQLErrorCodeSQLExceptionTranslator()
public SQLErrorCodeSQLExceptionTranslator(DataSource dataSource)
dataSource
- DataSource to use to find metadata 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 codesMethod Detail |
---|
public void setDataSource(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(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)
public void setSubclassTranslator(SQLExceptionTranslator subclassTranslator)
subclassTranslator
- custom subclass exception translator to useSQLException
public SQLExceptionTranslator getSubclassTranslator()
public void setFallbackTranslator(SQLExceptionTranslator fallback)
fallback
- custom fallback exception translator to use if error code
translation failsSQLStateSQLExceptionTranslator
public SQLExceptionTranslator getFallbackTranslator()
public DataAccessException translate(String task, String sql, SQLException sqlEx)
SQLExceptionTranslator
SQLException
into a generic DataAccessException
.
The returned DataAccessException is supposed to contain the original
SQLException
as root cause. However, client code may not generally
rely on this due to DataAccessExceptions possibly being caused by other resource
APIs as well. That said, a getRootCause() instanceof SQLException
check (and subsequent cast) is considered reliable when expecting JDBC-based
access to have happened.
translate
in interface SQLExceptionTranslator
task
- readable text describing the task being attemptedsql
- SQL query or update that caused the problem (may be null
)sqlEx
- the offending SQLException
SQLException
NestedRuntimeException.getRootCause()
protected String buildMessage(String task, String 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 SQLException
protected DataAccessException customTranslate(String task, String 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 SQLException
protected DataAccessException createCustomException(String task, String sql, SQLException sqlEx, 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 definition
CustomSQLErrorCodesTranslation.setExceptionClass(java.lang.Class)
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |