Spring’s DataAccessException
Discussion
In Spring, all DB Access Exception is subclasses of org.springframework.dao.DataAccessException. So you just try-catch what you have interest in instead of all DataAccessExceptions (I mean the sub-class of DataAccessException).
[1] DataAccessException Table
The following table lists some of the subclasses of DataAccessException.
DataAccessException | Is thrown when… |
CleanupFailureDataAccessException | An operation completes successfully, but an exception ocurs while cleaning up database resources. (ex. Closing a Connection) |
DataAccessResourceFailureException | A data access resource fails completely, such as not being able to connect to a database. |
DataIntegrityViolationException | An insert or update results in an integrity violation, such as a violation of a unique constraint. |
DataRetrievalFailureException | Certain data could not be retrieved, such as not finding a row by primary key. |
DeadlockLoserDataAccessException | The current process was a deadlock loser. |
IncorrectUpdateSemanticsDataAccessException | When something unintended happens on an update, such as updating more rows than expected. When this exception is thrown, the operation’s transaction has not been rolled back. |
InvalidDataAccessApiUsageException | A data access Java API is used incorrectly, such as failing to compile a query that must be compiled before execution. |
InvalidDataAccessResourceUsageException | A data access resource is used incorrectly, such as using bad SQL grammar to access a relational database. |
OptimisticLockingFailureException | There is an optimistic locking failure. This will be thrown by ORM tools or by custom DAO implementations. |
TypeMismatchDataAccessException | There is a mismatch between Java type and data type, such as trying to insert a String into a numeric database column. |
UncategorizedDataAccessException | Something goes wrong, but a more specific exception cannot be determined. |
[2] Hierarchy For org.springframework.dao.DataAccessException
see springframewrok1.2.9 javadoc
Package : | ||||||||
(1) | java.lang. | |||||||
(2) | org.springframework.core. | |||||||
(3) | org.springframework.dao. | |||||||
Exception Class Hierarchy : | ||||||||
Object (1) | ||||||||
Throwable | ||||||||
Exception | ||||||||
RuntimeException | ||||||||
NestedRuntimeException (2) | ||||||||
DataAccessException (3) | ||||||||
CleanupFailureDataAccessException | ||||||||
ConcurrencyFailureException | ||||||||
OptimisticLockingFailureException | ||||||||
PessimisticLockingFailureException | ||||||||
CannotAcquireLockException | ||||||||
CannotSerializeTransactionException | ||||||||
DeadlockLoserDataAccessException | ||||||||
DataAccessResourceFailureException | ||||||||
DataIntegrityViolationException | ||||||||
DataRetrievalFailureException | ||||||||
IncorrectResultSizeDataAccessException | ||||||||
InvalidDataAccessApiUsageException | ||||||||
InvalidDataAccessResourceUsageException | ||||||||
IncorrectUpdateSemanticsDataAccessException | ||||||||
TypeMismatchDataAccessException | ||||||||
UncategorizedDataAccessException | ||||||||
Spring’s Hibernate Exceptions class Hierarchy
org.springframework.dao | |
org.springframework.orm | |
org.springframework.orm.hibernate |
DataRetrievalFailureException | |||
ObjectRetrievalFailureException | |||
HibernateObjectRetrievalFailureException | |||
ConcurrencyFailureException | |||
OptimisticLockingFailureException | |||
ObjectOptimisticLockingFailureException | |||
HibernateOptimisticLockingFailureException | |||
InvalidDataAccessResourceUsageException | |||
HibernateQueryException | |||
UncategorizedDataAccessException | |||
HibernateJdbcException | |||
HibernateSystemException | |||
When using HibernateTemplate, its execute() method will be called everytime when you execute select, insert, update or delete operation.
HibernateException and SQLException will be catched within execute() method.
If HibernateException is catched, it will be converted to DataAccessException using convertHibernateAccessException() method in HibernateAccessor.
And SQLException will be converted to DataAccessException using convertJdbcAccessException() method in HibernateAccessor when catched.
Actually, when converting HibernateException, if HibernateException is instance of JDBCException, it will be treated as SQLException to convert (JDBCException.getSQLException is called as a method parameter).
When converting SQLException and JDBCException, SQLExceptionTranslator’s translate() method is called to translate SQLException to an appropriate DataAccessException.
more details to see HibernateTemplate, HibernateAccessor, SessionFactoryUtils, SQLExceptionTranslator, SQLErrorCodeSQLExceptionTranslator, SQLStateSQLExceptionTranslator source code
Table(1) Convert HibernateException to DataAccessException
net.sf.hibernate | org.springframework.orm.hibernate |
HibernateException | DataAccessException |
UnresolvableObjectException | HibernateObjectRetrievalFailureException |
ObjectNotFoundException | HibernateObjectRetrievalFailureException |
ObjectDeletedException | HibernateObjectRetrievalFailureException |
WrongClassException | HibernateObjectRetrievalFailureException |
StaleObjectStateException | HibernateOptimisticLockingFailureException |
QueryException | HibernateQueryException |
PersistentObjectException | InvalidDataAccessApiUsageException |
TransientObjectException | InvalidDataAccessApiUsageException |
Others | HibernateSystemException |
Spring’s JDBC Exceptions class Hierarchy
org.springframework.dao | |
org.springframework.jdbc |
InvalidDataAccessResourceUsageException | |||
BadSqlGrammarException | |||
DataAccessResourceFailureException | |||
CannotGetJdbcConnectionException | |||
InvalidDataAccessResourceUsageException | |||
InvalidResultSetAccessException | |||
InvalidDataAccessResourceUsageException | |||
IncorrectUpdateSemanticsDataAccessException | |||
JdbcUpdateAffectedIncorrectNumberOfRowsException | |||
DataRetrievalFailureException | |||
LobRetrievalFailureException | |||
UncategorizedDataAccessException | |||
SQLWarningException | |||
UncategorizedSQLException | |||
Before looking Table(2)
SQLExceptionTranslator is in interface, default SQLErrorCodeSQLExceptionTranslator and SQLStateSQLExceptionTranslator implement it.
Within SQLErrorCodeSQLExceptionTranslator’s translate() method, customer translator will be looked for first. That means customer translator will be userd first.
Next, the DataAccessionException listed in Table(2) will be thrown against the grouped error codes.
If no error codes are available, SQLStateSQLExceptionTranslator’s translate() method is called to handle this.
Table(2) Convert SQLException to DataAccessException
java.sql |
org.springframework.jdbc (1) org.springframework.dao (2) |
SQLException | Spring’s JDBC Exceptions DataAccessException |
SQLException | BadSqlGrammarException (1) |
InvalidResultSetAccessException (1) | |
DataAccessResourceFailureException (2) | |
DataIntegrityViolationException (2) | |
CannotAcquireLockException (2) | |
DeadlockLoserDataAccessException (2) | |
CannotSerializeTransactionException (2) | |
According to SQLState, BadSqlGrammarException or DataIntegrityViolationException will be thrown in SQLStateSQLExceptionTranslator’s translate() method.
Otherwise, UncategorizedSQLException will be thrown. (Not able to diagnose all problems, , but is portable between databases anddoes need require special initialization (no database vendor detection etc). by Rod Johnson and Juergen Hoeller)