【问题标题】:ejb jta nullpointer exception not rolling backejb jta 空指针异常不回滚
【发布时间】:2014-05-19 21:42:43
【问题描述】:

我有一个 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) 发生时,事务不会回滚并且客户实体被持久化,但是当我用

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

【问题讨论】:

    标签: java transactions ejb


    【解决方案1】:

    在第二个示例中,NullPointerException 未被捕获,相反,您正在捕获 EJBException,它是其他运行时异常类。

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 2012-12-28
      • 2014-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多