Top Description Fields Constructors Methods
org.springframework.jdbc.support

public Class SQLStateSQLExceptionTranslator

extends AbstractFallbackSQLExceptionTranslator
Class Inheritance
Imports
java.sql.SQLException, java.util.Set, org.springframework.dao.CannotAcquireLockException, .DataAccessException, .DataAccessResourceFailureException, .DataIntegrityViolationException, .DuplicateKeyException, .PessimisticLockingFailureException, .QueryTimeoutException, .TransientDataAccessResourceException, org.springframework.jdbc.BadSqlGrammarException, org.springframework.lang.Nullable

SQLExceptionTranslator implementation that analyzes the SQL state in the SQLException based on the first two digits (the SQL state "class"). Detects standard SQL state values and well-known vendor-specific SQL states.

Not able to diagnose all problems, but is portable between databases and does not require special initialization (no database vendor detection, etc.). For more precise translation, consider SQLErrorCodeSQLExceptionTranslator.

This translator is commonly used as a fallback behind a primary translator such as SQLErrorCodeSQLExceptionTranslator or SQLExceptionSubclassTranslator.

Authors
Rod Johnson, Juergen Hoeller, Thomas Risberg
See Also
java.sql.SQLException#getSQLState(), SQLErrorCodeSQLExceptionTranslator, SQLExceptionSubclassTranslator

Field Summary

Modifier and TypeField and Description
private static final Set<String>
private static final Set<String>
private static final Set<String>
private static final Set<Integer>
private static final Set<String>
private static final Set<String>
Inherited from org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected DataAccessException
doTranslate(String
readable text describing the task being attempted
task
,
String
the SQL query or update that caused the problem (if known)
sql
,
SQLException
the offending SQLException
ex
)

Implements abstract org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.doTranslate.

Template method for actually translating the given exception.

private String

Returns:

the SQL state code
getSqlState
(SQLException
the exception from which the SQL state is to be extracted
ex
)

Gets the SQL state code from the supplied exception.

pack-priv static boolean
indicatesCannotAcquireLock(String
the SQL state value
sqlState
)

Check whether the given SQL state indicates a CannotAcquireLockException, with SQL state 40001 as a specific indication.

pack-priv static boolean
indicatesDuplicateKey(String
the SQL state value
sqlState
,
int
the error code
errorCode
)

Check whether the given SQL state and the associated error code (in case of a generic SQL state value) indicate a DuplicateKeyException: either SQL state 23505 as a specific indication, or the generic SQL state 23000 with a well-known vendor code.

Inherited from org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator:
buildMessagegetCustomTranslatorgetFallbackTranslatorsetCustomTranslatorsetFallbackTranslatortranslate

Field Detail

BAD_SQL_GRAMMAR_CODESback to summary
private static final Set<String> BAD_SQL_GRAMMAR_CODES
DATA_ACCESS_RESOURCE_FAILURE_CODESback to summary
private static final Set<String> DATA_ACCESS_RESOURCE_FAILURE_CODES
DATA_INTEGRITY_VIOLATION_CODESback to summary
private static final Set<String> DATA_INTEGRITY_VIOLATION_CODES
DUPLICATE_KEY_ERROR_CODESback to summary
private static final Set<Integer> DUPLICATE_KEY_ERROR_CODES
PESSIMISTIC_LOCKING_FAILURE_CODESback to summary
private static final Set<String> PESSIMISTIC_LOCKING_FAILURE_CODES
TRANSIENT_DATA_ACCESS_RESOURCE_CODESback to summary
private static final Set<String> TRANSIENT_DATA_ACCESS_RESOURCE_CODES

Constructor Detail

SQLStateSQLExceptionTranslatorback to summary
public SQLStateSQLExceptionTranslator()

Method Detail

doTranslateback to summary
protected DataAccessException doTranslate(String task, String sql, SQLException ex)

Implements abstract org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.doTranslate.

Doc from org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.doTranslate.

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.

Parameters
task:String

readable text describing the task being attempted

sql:String

the SQL query or update that caused the problem (if known)

ex:SQLException

the offending SQLException

Returns:DataAccessException

the DataAccessException, wrapping the SQLException; or null if no exception match found

Annotations
@Override
@Nullable
getSqlStateback to summary
private String getSqlState(SQLException ex)

Gets the SQL state code from the supplied exception.

Some JDBC drivers nest the actual exception from a batched update, so we might need to dig down into the nested exception.

Parameters
ex:SQLException

the exception from which the SQL state is to be extracted

Returns:String

the SQL state code

Annotations
@Nullable
indicatesCannotAcquireLockback to summary
pack-priv static boolean indicatesCannotAcquireLock(String sqlState)

Check whether the given SQL state indicates a CannotAcquireLockException, with SQL state 40001 as a specific indication.

Parameters
sqlState:String

the SQL state value

indicatesDuplicateKeyback to summary
pack-priv static boolean indicatesDuplicateKey(String sqlState, int errorCode)

Check whether the given SQL state and the associated error code (in case of a generic SQL state value) indicate a DuplicateKeyException: either SQL state 23505 as a specific indication, or the generic SQL state 23000 with a well-known vendor code.

Parameters
sqlState:String

the SQL state value

errorCode:int

the error code