Can Hibernate data access throw exceptions similar to Spring DeadlockLoserDataAccessException or other TransientDataAccessException?
I'm currently working on a project that uses data access on different databases. Our main database is accessed through Hibernate (either via the Criteria
framework, or HQL queries), but we also have accesses to other dbs using plain JDBC / SQL queries (via Spring-Jdbc).
For some of our JDBC开发者_如何学编程 calls, we had to deal with the possibility of the DAO layer throwing some flavours of the Spring runtime TransientDataAccessException
, like DeadlockLoserDataAccessException
, or CannotAcquireLockException
.
My question: should we plan for similar exceptions thrown by the Hibernate DAOs? It's very difficult to write tests that would exhibit such exceptions, and I don't want to build support for these if they cannot be thrown. And if they can, which exceptions exactly? What do you think?
Exceptions thrown from your Spring backed persistence implementation are not really rooted in Spring => they are data access exceptions.
Hence you can get similar exceptions from a pure Hibernate implementation e.g:
LockAcquisitionException: indicating a problem acquiring lock on the database
Having said that, testing for these exceptions specifically would not be wise. Hence they are RuntimeException
s. If you getting these exceptions from your Spring backed implementation, I would rather focus on solving the problem.
Take a look at the hibernate Session api, http://docs.jboss.org/hibernate/annotations/3.5/api/index.html?org/hibernate/Session.html you; specifically, createQuery and createCriteria.
createQuery, which takes an hql string, returns a Query object and throws a HibernateException. createCriteria returns a Criteria object with no exceptions thrown. Calling .list() on a Criteria or Query object throws a HibernateException.
Most of the function calls, if they throw an exception, throw a HibernateException. There are a few special cases like calling uniqueResult() on a Query object, which throws a NonUniqueResultException, in addition to a HibernateException.
精彩评论