我有一个 EJB 无状态 session 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)发生时,事务不会回滚并且客户实体被持久化,但是当我用
捕获异常时 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导致回滚。
最佳答案
在第二个示例中,未捕获 NullPointerException
,而是捕获 EJBException
,它是其他运行时异常类。
正如您所说,当容器拦截 NullPointerException
时,事务将被标记为回滚。
第一个示例捕获Exception
,它是一个基类(Java 异常是分层的),
因此,Exception
的任何子类,例如 NullPointer
或 EJBException
被捕获了。在这种情况下,容器不会将事务标记为回滚。
关于java - ejb jta空指针异常不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23747702/