jakarta-ee - 当 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" 选项。
对这个问题有什么想法吗?
解决方案
您需要从 EJB 中抛出异常,而不是在其中捕获它。将 BeanC 更改为以下实现。
@Stateless
public class BeanC {
public void handle() {
throw new RuntimeException("error occurred!!");
}
}
推荐阅读
- swift - 推送视图控制器未出现在标签栏控制器下方
- java - 如何在 OSGI (Apache Felix) 中为别人的 bundle 实现 AOP?
- dialogflow-es - 昨天有效的操作现在确实崩溃了
- c - 我怎样才能使新结构与旧结构兼容?
- c# - Serilog 的显式日志级别而不是 MinimumLevel
- python - 如何用opencv正确叠加两个图像
- r - 使用 set.seed 命令的不同样本结果?
- java - 是否有 INDArray 的方法来执行两个 INDArray(具有不同大小)的相加?
- database - 在 node-oracledb 中使用事务
- c# - 无法从 c# 代码调用类模板的非虚拟函数