我最近一直面临一个奇怪的问题。我试图优雅地处理陈旧状态异常。但是在 catch 块中它仍然抛出异常。以下是代码片段
public void saveObject(Object ob){
try{
sessionFactory.getCurrentSession().saveOrUpdate(ob);
}catch(org.springframework.orm.hibernate5.HibernateOptimisticLockingFailureException e){
object latestObject = // get latest object from db;
copyFieldsFromObToLatestObject(ob,latestObject);
// print the version of of both object
LOGGER.info(" ui_version="+ob.getVersion().longValue()+"
entity_version="+latestObject.getVersion().longValue());
// the ui _version is less than entity_ version as expected
sessionFactory.getCurrentSession().saveOrUpdate(latestObject); // at this line I still get the same optimistic locking exception
}
}
/**
ob2 is the latest object which contains the correct version hence copying the fields from previous object to this latest object
**/
private void copyFieldsFromObToLatestObject(ob1,ob2){
ob2.setA(ob1.getA())..
so on
}
有人可以看看这个。我无法得到正确处理后仍然抛出乐观锁定异常的原因
编辑 1:堆栈跟踪:
org.springframework.orm.hibernate5.HibernateOptimisticLockingFailureException:批量更新从更新[0]返回了意外的行数;实际行数:0;预期:1;嵌套异常是 org.hibernate.StaleStateException:批量更新从更新 [0] 返回了意外的行数;实际行数:0;预期:org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:283) 中的 1 [INFO] [INFO] org.springframework.orm.hibernate5.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:755) [INFO]在 org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:590) [INFO] 在 org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.