首页 > 解决方案 > 当 RuntimeException 在另一个 bean 中抛出时,JTA 不回滚事务?

问题描述

我们有 3 个 EJB 来处理我们的业务,如下所示:

@Stateless
public class BeanA {

    @PersistenceContext(unitName = "primary")
    private EntityManager em;

    @EJB private BeanB beanB;

    public void handle() {

      EntityA entity= new EntityA();
      ....

      beanB.handle();

      em.persist(entity); //Transaction does not rollback and saved entity


    }
 }  

@Stateless
public class BeanB {

     @EJB private BeanC beanC;

     public void handle() {
        beanC.handle();
     }
}

@Stateless
public class BeanC {

     public void handle() {

        try {
           throw new RunTimeException("error occurred!!");       
        } catch(RunTimeException e) {
            e.printStacktrace();

        }
     }
}

根据JTA概念,如果发生RuntimeExcpetion当前事务将被回滚,我预计上述事务应该回滚但不回滚。我们还使用 Wildfly10 作为应用程序服务器和数据源中的活动 JTA="true" 选项。

对这个问题有什么想法吗?

标签: jakarta-eejtawildfly-10

解决方案


您需要从 EJB 中抛出异常,而不是在其中捕获它。将 BeanC 更改为以下实现。

@Stateless
public class BeanC {

     public void handle() {

          throw new RuntimeException("error occurred!!");       
     }
}

推荐阅读