2015-01-04 101 views
0

我使用spring orm with hibernate.I有如下方法樂觀版本鎖定

對象具有版本列。@版本。

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? 
+1

爲什麼你不測試它? – 2015-01-04 16:36:51

回答

0

底層的ORM(休眠),將拋出

org.hibernate.StaleObjectStateException 

那麼Spring數據訪問層會抓住它,並把它包在

org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException 

所以堆棧跟蹤,你會得到的將成爲:

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

You ca n閱讀約hibernate optimistic lockingspring exception translation

+0

何時拋出此異常,然後http://docs.spring.io/spring-framework/docs/2.0.8/api/org/springframework/dao/ConcurrencyFailureException.html – user93796 2015-01-04 18:32:47

+0

ConcurrencyFailureException是HibernateOptimisticLockingFailureException的超類。請參閱http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.html – outdev 2015-01-04 20:29:29