Reputation: 5685
spring template will wrap hibernate exceptions into DataAccessExceptions
. so trying to directly capture hibernate exceptions won't work. like this
try{
springdao.update(row)
}
catch(HibernateException e) {
//won't do
}
questions:
what's the best practice to capture DataAccessException wrapped hibernate exception ? just catch(DataAccessException)
?
even though spring has wrapped that exception, when it is thrown. it exhibits as just original hibernate exception. like below, why ?
Could not synchronize database state with session org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):
Upvotes: 0
Views: 1429
Reputation: 6932
I think its better to handle DataAccessException
instead of HibernateException
or SQLException
. Because it describes the issue in more generalized way. In case if you change your ORM/ Database later, it will handle your database related exception handling in same manner. And also it contains the same exception as that of Hibernate. Even in case of Non-SQL databases, same exception is thrown
Spring documentation says:
This exception hierarchy aims to let user code find and handle the kind of error encountered without knowing the details of the particular data access API in use (e.g. JDBC). Thus it is possible to react to an optimistic locking failure without knowing that JDBC is being used.
Upvotes: 1