2014-05-19 65 views
0

我有一個EJB Stateless Session Bean的,像這樣:EJB JTA空指針異常不回滾

public void persist(Customer customer,Child child){ 
try{ 
    em.persist(customer); 
    Father father = new Father(); 
    father.setChild(child); here child is null 
    em.persist(father); 
    }catch(Exception e){ 

    }  
} 

當異常(NullPointerException異常)出現交易不回滾和客戶實體堅持着,但是當我趕上與

public void persist(Customer customer,Child child){ 
    try{ 
    em.persist(customer); 
    Father father = new Father(); 
    father.setChild(child); here child is null 
    em.persist(father); 
    }catch(EJBException e){ 

    }  
} 

的事務回滾異常,但我不明白爲什麼,NullPointerException異常延伸RuntimeException.The文檔說一個RuntimeException導致回滾。

回答

0

在第二個示例中,不捕獲NullPointerException,而是捕獲EJBException這是其他運行時異常類。

正如你所說,當Container攔截NullPointerException時,事務被標記爲回滾。

第一個例子捕捉Exception,至極是基類(Java異常是分級的),因此 ,的ExceptionNullPointerEJBException 落入任何亞類。在這種情況下,容器不會將事務標記爲回滾。