类 SQLErrorCodeSQLExceptionTranslator
- java.lang.Object
- org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator
- org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
- 所有已实现的接口:
SQLExceptionTranslator
public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExceptionTranslator
Implementation ofSQLExceptionTranslator
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:
- Try custom translation implemented by any subclass. Note that this class is concrete and is typically used itself, in which case this rule doesn't apply.
- Apply error code matching. Error codes are obtained from the SQLErrorCodesFactory by default. This factory loads a "sql-error-codes.xml" file from the class path, defining error code mappings for database names from database meta-data.
- Fallback to a fallback translator.
SQLStateSQLExceptionTranslator
is the default fallback translator, analyzing the exception's SQL state only. On Java 6 which introduces its ownSQLException
subclass hierarchy, we will useSQLExceptionSubclassTranslator
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.
- 作者:
- Rod Johnson, Thomas Risberg, Juergen Hoeller
- 另请参阅:
SQLErrorCodesFactory
,SQLStateSQLExceptionTranslator
字段概要
从类继承的字段 org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator
logger
构造器概要
构造器 构造器 说明 SQLErrorCodeSQLExceptionTranslator()
Constructor for use as a JavaBean.SQLErrorCodeSQLExceptionTranslator(String dbName)
Create a SQL error code translator for the given database product name.SQLErrorCodeSQLExceptionTranslator(DataSource dataSource)
Create a SQL error code translator for the given DataSource.SQLErrorCodeSQLExceptionTranslator(SQLErrorCodes sec)
Create a SQLErrorCode translator given these error codes.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected DataAccessException
createCustomException(String task, String sql, SQLException sqlEx, Class<?> exceptionClass)
Create a customDataAccessException
, based on a given exception class from aCustomSQLErrorCodesTranslation
definition.protected DataAccessException
customTranslate(String task, String sql, SQLException sqlEx)
Subclasses can override this method to attempt a custom mapping fromSQLException
toDataAccessException
.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.从类继承的方法 org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator
buildMessage, getFallbackTranslator, setFallbackTranslator, translate
构造器详细资料
SQLErrorCodeSQLExceptionTranslator
public SQLErrorCodeSQLExceptionTranslator()
Constructor for use as a JavaBean. The SqlErrorCodes or DataSource property must be set.
SQLErrorCodeSQLExceptionTranslator
public SQLErrorCodeSQLExceptionTranslator(DataSource dataSource)
Create a SQL error code translator for the given DataSource. Invoking this constructor 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 usable- 另请参阅:
SQLErrorCodesFactory
SQLErrorCodeSQLExceptionTranslator
public SQLErrorCodeSQLExceptionTranslator(String dbName)
Create a SQL error code translator for the given database product name. Invoking this constructor will avoid obtaining a Connection from the DataSource to get the meta-data.- 参数:
dbName
- the database product name that identifies the error codes entry- 另请参阅:
SQLErrorCodesFactory
,DatabaseMetaData.getDatabaseProductName()
SQLErrorCodeSQLExceptionTranslator
public SQLErrorCodeSQLExceptionTranslator(SQLErrorCodes sec)
Create a SQLErrorCode translator given these error codes. Does not require a database meta-data lookup to be performed using a connection.- 参数:
sec
- error codes
方法详细资料
setDataSource
public void setDataSource(DataSource dataSource)
Set the DataSource for this translator.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 usable- 另请参阅:
SQLErrorCodesFactory.getErrorCodes(javax.sql.DataSource)
,DatabaseMetaData.getDatabaseProductName()
setDatabaseProductName
public void setDatabaseProductName(String dbName)
Set the database product name for this translator.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 entry- 另请参阅:
SQLErrorCodesFactory.getErrorCodes(String)
,DatabaseMetaData.getDatabaseProductName()
setSqlErrorCodes
public void setSqlErrorCodes(SQLErrorCodes sec)
Set custom error codes to be used for translation.- 参数:
sec
- custom error codes to use
getSqlErrorCodes
public SQLErrorCodes getSqlErrorCodes()
Return the error codes used by this translator. Usually determined via a DataSource.
doTranslate
protected DataAccessException doTranslate(String task, String sql, SQLException ex)
Template method for actually translating the given exception.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
在类中AbstractFallbackSQLExceptionTranslator
- 参数:
task
- readable text describing the task being attemptedsql
- the SQL query or update that caused the problem (if known)ex
- the offendingSQLException
- 返回:
- the DataAccessException, wrapping the
SQLException
; ornull
if no exception match found
customTranslate
protected DataAccessException customTranslate(String task, String sql, SQLException sqlEx)
Subclasses can override this method to attempt a custom mapping fromSQLException
toDataAccessException
.- 参数:
task
- readable text describing the task being attemptedsql
- the SQL query or update that caused the problem (may benull
)sqlEx
- the offending SQLException- 返回:
null
if no custom translation applies, otherwise aDataAccessException
resulting from custom translation. This exception should include thesqlEx
parameter as a nested root cause. This implementation always returnsnull
, meaning that the translator always falls back to the default error codes.
createCustomException
protected DataAccessException createCustomException(String task, String sql, SQLException sqlEx, Class<?> exceptionClass)
Create a customDataAccessException
, based on a given exception class from aCustomSQLErrorCodesTranslation
definition.- 参数:
task
- readable text describing the task being attemptedsql
- the SQL query or update that caused the problem (may benull
)sqlEx
- the offending SQLExceptionexceptionClass
- the exception class to use, as defined in theCustomSQLErrorCodesTranslation
definition- 返回:
null
if the custom exception could not be created, otherwise the resultingDataAccessException
. This exception should include thesqlEx
parameter as a nested root cause.- 另请参阅:
CustomSQLErrorCodesTranslation.setExceptionClass(java.lang.Class<?>)