有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java CannotAcquireLockException+LockAcquisitionException

我正在调查我的Spring Data+Hibernate应用程序中的一个问题。我在日志中发现了这个问题,但我试图理解为什么hibernate异常org.hibernate.exception.LockAcquisitionException会抛出Spring org.springframework.dao.CannotAcquireLockException

对于异常报告,我希望我的异常保持一致,并抛出所有Hibernate异常。这可能吗

Caught unhandled exceptionorg.springframework.dao.CannotAcquireLockException: could not update: [com.database.model.MyTable#22791]; SQL [update MyTable set x=1, etc]; nested exception is
org.hibernate.exception.LockAcquisitionException: could not update:[com.database.model.MyTable#22791]#  at
org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:246)#   at
org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221)#  at
org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)# at
org.springframework.transaction.support.AbstractPlatformTr

共 (2) 个答案

  1. # 1 楼答案

    why a hibernate exception org.hibernate.exception.LockAcquisitionException can throw a Spring org.springframework.dao.CannotAcquireLockException?

    没有。hibernate异常被捕获,然后抛出一个Spring异常

    For exception reporting, I would like my exceptions to be consistent and to throw all Hibernate exceptions. Is this possible?

    我不确定我是否理解为什么这比记录Spring异常更加一致,或者实际上是有益的。所有异常都继承自Throwable,它提供了对其原因的访问,应该为由其他异常引起的所有Spring异常正确设置该原因。因此,您当然可以在原因链中导航,以查找要记录的异常。您甚至可能会发现各种Spring异常中的一些方法很有用,例如getMostSpecificCause

  2. # 2 楼答案

    Hibernate的LockAcquisitionException不会抛出Spring的CannotAcquireLockException。任何带有@repository注释的Spring存储库(我相信Spring数据存储库天生就是这样)都将带有Spring异常转换。这样,您的数据层就不会泄漏,并且都被常见的Spring异常所包装。请参阅Spring关于PersistenceExceptionTranslator类或异常转换的javadocs

    要禁用转换,请查看以下线程:How to deactivate Spring Data exception translation