【问题标题】:CannotAcquireLockException + LockAcquisitionException无法获取锁定异常 + 锁定获取异常
【发布时间】:2017-05-10 14:47:14
【问题描述】:

我正在调查我在 Spring Data + 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

【问题讨论】:

    标签: java hibernate spring-data


    【解决方案1】:

    为什么休眠异常 org.hibernate.exception.LockAcquisitionException 可以抛出 Spring org.springframework.dao.CannotAcquireLockException

    它没有。捕获休眠异常,然后抛出 Spring 异常。

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

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

    【讨论】:

      【解决方案2】:

      Hibernate 的 LockAcquisitionException 不会抛出 Spring 的 CannotAcquireLockException。任何使用 @Repository 注释的 Spring 存储库(我相信 Spring Data 存储库天生就是这样)都将附带 Spring 异常翻译。这样您的数据层就不会泄漏,并且全部被常见的 Spring 异常包裹。请参阅 Spring 的有关 PersistenceExceptionTranslator 类或异常翻译的 javadocs。

      要禁用翻译,可以查看这个帖子:How to deactivate Spring Data exception translation

      【讨论】:

        猜你喜欢
        • 2015-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-18
        • 2018-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多