Class SQLErrorCodeSQLExceptionTranslator
- All Implemented Interfaces:
SQLExceptionTranslator
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:
- 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.
SQLStateSQLExceptionTranslatoris the default fallback translator, analyzing the exception's SQL state only. Since Java 6 which introduces its ownSQLExceptionsubclass hierarchy, we useSQLExceptionSubclassTranslatorby 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 (for example, in the "/WEB-INF/classes" directory), as long as the Spring JDBC package is loaded from the same ClassLoader.
This translator is commonly used by default if a user-provided `sql-error-codes.xml`
file has been found in the root of the classpath, as a signal to use this strategy.
Otherwise, SQLExceptionSubclassTranslator serves as the default translator
as of 6.0.
- Author:
- Rod Johnson, Thomas Risberg, Juergen Hoeller, Sam Brannen
- See Also:
-
Field Summary
Fields inherited from class org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator
logger -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for use as a JavaBean.Create an SQL error code translator for the given database product name.SQLErrorCodeSQLExceptionTranslator(DataSource dataSource) Create an SQL error code translator for the given DataSource.Create an SQLErrorCode translator given these error codes. -
Method Summary
Modifier and TypeMethodDescriptionprotected @Nullable DataAccessExceptioncreateCustomException(String task, @Nullable String sql, SQLException sqlEx, Class<?> exceptionClass) Create a customDataAccessException, based on a given exception class from aCustomSQLErrorCodesTranslationdefinition.protected @Nullable DataAccessExceptioncustomTranslate(String task, @Nullable String sql, SQLException sqlEx) Deprecated.protected @Nullable DataAccessExceptiondoTranslate(String task, @Nullable String sql, SQLException ex) Template method for actually translating the given exception.Return the error codes used by this translator.voidsetDatabaseProductName(String dbName) Set the database product name for this translator.voidsetDataSource(DataSource dataSource) Set the DataSource for this translator.voidSet custom error codes to be used for translation.Methods inherited from class org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator
buildMessage, getCustomTranslator, getFallbackTranslator, setCustomTranslator, setFallbackTranslator, translate
-
Constructor Details
-
SQLErrorCodeSQLExceptionTranslator
public SQLErrorCodeSQLExceptionTranslator()Constructor for use as a JavaBean. The SqlErrorCodes or DataSource property must be set. -
SQLErrorCodeSQLExceptionTranslator
Create an 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.- Parameters:
dataSource- the DataSource to use to find meta-data and establish which error codes are usable- See Also:
-
SQLErrorCodeSQLExceptionTranslator
Create an 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.- Parameters:
dbName- the database product name that identifies the error codes entry- See Also:
-
SQLErrorCodeSQLExceptionTranslator
Create an SQLErrorCode translator given these error codes. Does not require a database meta-data lookup to be performed using a connection.- Parameters:
sec- error codes
-
-
Method Details
-
setDataSource
Set the DataSource for this translator.Setting this property will cause a Connection to be obtained from the DataSource to get the meta-data.
- Parameters:
dataSource- the DataSource to use to find meta-data and establish which error codes are usable- See Also:
-
setDatabaseProductName
Set the database product name for this translator.Setting this property will avoid obtaining a Connection from the DataSource to get the meta-data.
- Parameters:
dbName- the database product name that identifies the error codes entry- See Also:
-
setSqlErrorCodes
Set custom error codes to be used for translation.- Parameters:
sec- custom error codes to use
-
getSqlErrorCodes
Return the error codes used by this translator. Usually determined via a DataSource.- See Also:
-
doTranslate
protected @Nullable DataAccessException doTranslate(String task, @Nullable String sql, SQLException ex) Description copied from class:AbstractFallbackSQLExceptionTranslatorTemplate method for actually translating the given exception.The passed-in arguments will have been pre-checked. Furthermore, this method is allowed to return
nullto indicate that no exception match has been found and that fallback translation should kick in.- Specified by:
doTranslatein classAbstractFallbackSQLExceptionTranslator- Parameters:
task- readable text describing the task being attemptedsql- the SQL query or update that caused the problem (if known)ex- the offendingSQLException- Returns:
- the DataAccessException, wrapping the
SQLException; ornullif no exception match found
-
customTranslate
@Deprecated(since="6.1") protected @Nullable DataAccessException customTranslate(String task, @Nullable String sql, SQLException sqlEx) Deprecated.Subclasses can override this method to attempt a custom mapping fromSQLExceptiontoDataAccessException.- Parameters:
task- readable text describing the task being attemptedsql- the SQL query or update that caused the problem (may benull)sqlEx- the offending SQLException- Returns:
nullif no custom translation applies, otherwise aDataAccessExceptionresulting from custom translation. This exception should include thesqlExparameter as a nested root cause. This implementation always returnsnull, meaning that the translator always falls back to the default error codes.
-
createCustomException
protected @Nullable DataAccessException createCustomException(String task, @Nullable String sql, SQLException sqlEx, Class<?> exceptionClass) Create a customDataAccessException, based on a given exception class from aCustomSQLErrorCodesTranslationdefinition.- Parameters:
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 theCustomSQLErrorCodesTranslationdefinition- Returns:
nullif the custom exception could not be created, otherwise the resultingDataAccessException. This exception should include thesqlExparameter as a nested root cause.- See Also:
-
AbstractFallbackSQLExceptionTranslator.setCustomTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)