HibernateOptimisticLockingFailureException将连接标记为“关闭”?

7

我得到了以下堆栈跟踪:

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.btfin.wrapcore.request.MFRequest] with identifier [2850448]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.btfin.wrapcore.request.MFRequest#2850448]
  at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:672)
  at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)

由于乐观锁的异常,这是导致问题的根本原因。我可以解决这个问题。
我的问题是,在这种情况下,异常处理将数据库连接设置为“关闭”。(这会影响我的连接池)。
如何处理通过Spring和Hibernate上升并返回关闭连接的 HibernateOptimisticLockingFailureException 等数据库异常的模式?
您知道Spring / Hibernate代码中设置连接为关闭的部分吗?
1个回答

6

Hibernate文档明确说明,如果在使用Session时出现任何异常,那么该Session之后就无法再被重复使用。此外,每个Session可以包含多个事务,在每个事务提交后,同样会关闭连接。

但是在使用连接池时,当调用close()方法时,连接并没有真正关闭,而是返回到连接池中

当应用程序关闭其连接时,底层物理连接被回收而不是被关闭。

因此,如果您遇到连接被物理关闭的问题,我建议更加关注连接池,而不是Hibernate或Spring - 它们不能做更多的事情,只能调用close()方法,其工作方式如我之前所描述的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接