乐观版本锁定



我正在使用带有休眠功能的 spring orm。我有一个方法如下

对象具有版本列 .@Version。

    void processObject(){
      Object obj = getObjectFromDB(int id);
      //do lot of processing. Takes 15 min
     //version number is not changed    
     //if some other object updates the same object , which
     //exception is thrown when folloing code runs
     updateObject(obj) ; 
     //
    }
    @Transactional
    updateObject(Object object){
     session.save(object)
    }
    @Transcational
    Object getObjectFromDB(int id){
    }

现在,如果在我处理和保存对象时其他线程更新对象,那么将抛出哪个异常?

1)StaleStateException (hibernate)
2)StaleObjectstateException (hibernate)
3)ConcurrentFailureException (spring)
4)Any other?

底层orm(休眠)将抛出

org.hibernate.StaleObjectStateException 

然后 spring 数据访问层将捕获它,并将其包装在

org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException

因此,您将获得的堆栈跟踪将是:

org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException: 
    Caused by: org.hibernate.StaleObjectStateException: 

您可以阅读有关休眠乐观锁定和 Spring 异常转换的信息

最新更新