ejb jta nullpointer异常没有回滚

时间:2014-05-19 21:42:44

标签: java transactions ejb

我有一个EJB无状态会话Bean,如下所示:

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)时,事务不回滚且Customer实体被持久化,但当我用

捕获异常时
   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。文档说RuntimeException导致回滚。

1 个答案:

答案 0 :(得分:0)

在第二个示例中,未捕获NullPointerException,而是捕获EJBException,这是其他运行时异常类。

正如您所说,当Container拦截NullPointerException时,事务被标记为回滚。

第一个示例捕获Exception,它是一个基类(Java异常是分层的), 因此,Exception的任何子类,例如NullPointerEJBException 被抓住了。在这种情况下,Container不会标记事务以进行回滚。

相关问题