Hibernate数据访问是否会抛出类似于Spring DeadlockLoserDataAccessException或其他TransientDataAccessException的异常?
我目前正在开发一个在不同数据库上使用数据访问的项目。我们的主数据库是通过 Hibernate 访问的(通过 Criteria 框架或 HQL 查询),但我们也可以使用普通 JDBC / SQL 查询(通过 Spring-Jdbc)访问其他数据库。
对于我们的一些 JDBC 调用,我们必须处理 DAO 层抛出某些 Spring 运行时 TransientDataAccessException
的可能性,例如 DeadlockLoserDataAccessException
或 CannotAcquireLockException< /代码>。
我的问题:我们是否应该为 Hibernate DAO 抛出的类似异常做好计划?编写会表现出此类异常的测试非常困难,如果无法抛出这些异常,我不想构建对这些异常的支持。如果可以的话,具体有哪些例外?你怎么认为?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Spring 支持的持久性实现中抛出的异常并不是真正根源于 Spring => 。它们是数据访问异常。
因此,您可以从纯Hibernate实现中获得类似的异常,例如:
LockAcquisitionException:指示获取数据库锁定时出现问题话
虽如此,专门测试这些异常并不明智。因此它们是 RuntimeException。如果您从 Spring 支持的实现中得到这些异常,我宁愿专注于解决问题。
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.看一下 hibernate Session api, http://docs.jboss.org/hibernate/annotations/3.5/api/index.html?org/hibernate/Session.html 你;具体来说,createQuery 和 createCriteria。
createQuery,它接受一个 hql 字符串,返回一个 Query 对象并抛出一个 HibernateException。 createCriteria 返回一个 Criteria 对象,不抛出任何异常。在 Criteria 或 Query 对象上调用 .list() 会引发 HibernateException。
大多数函数调用,如果抛出异常,就会抛出 HibernateException。有一些特殊情况,例如在 Query 对象上调用 uniqueResult(),除了 HibernateException 之外,还会引发 NonUniqueResultException。
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.