我得到以下堆栈跟踪:
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)
这是由于乐观锁定异常。我可以解决这个问题的根本原因。
我的问题是 - 在这种情况下 - 异常处理将数据库连接设置为“关闭”。 (这会导致我的连接池出现问题)。
处理像
HibernateOptimisticLockingFailureException
这样的数据库异常的模式是什么?通过 Spring 和 hibernate 冒泡并返回关闭的连接?您知道 Spring/Hibernate 代码中将连接设置为关闭的部分吗?
最佳答案
Hibernate Docs明确声明,如果在使用 Session 时发生任何异常,则 Session 之后不能重用。此外,每个 Session 可以包含多个事务,并且在每个事务提交后 - 同样发生,连接关闭。
但是在使用连接池时,连接并没有真正关闭,当调用 close() 方法时,connection is returned to the pool without physical closing :
when an application closes its connection, the underlying physical connection is recycled rather than being closed.
因此,如果您在物理关闭连接时遇到问题,我宁愿更多地关注池,而不是 Hibernate 或 Spring - 它们只能调用 close() ,这应该像我之前描述的那样工作。
关于spring - HibernateOptimisticLockingFailureException 将连接标记为 'closed' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10406746/