gpt4 book ai didi

java - 运行时异常是@ApplicationException(rollback=false) 但事务最终回滚

转载 作者:行者123 更新时间:2023-12-01 11:33:17 25 4
gpt4 key购买 nike

我的运行时异常

@ApplicationException(rollback=false)
public class UncheckedException extends RuntimeException {}

我的EJB代码

@Stateless
@Transactional(TxType.REQUIRES_NEW)
public class ContainerManagedTransactionBean {

@PersistenceContext EntityManager em;

public void insertAndThrowUnchecked() throws UncheckedException {
em.persist(new Entry());
throw new UncheckedException();
}

}

我的另一个 EJB 是客户端

@Singleton
@Startup
@Transactional(TxType.NOT_SUPPORTED)
public class Start {

@EJB
ContainerManagedTransactionBean bean;

@PostConstruct
public void start() {
//...
try {
bean.insertAndThrowUnchecked();
} catch (Exception e) {
System.out.println("Start unchecked exception catched");
}
}

有人可以解释一下为什么 insertAndThrowUnchecked 被回滚吗?

在类似的情况下,当检查异常时,

@ApplicationException(rollback=false)
public class CheckedException extends Exception {}

交易已提交。

工作示例位于 this GitHub link .

我希望得到清晰的解释并链接到 EJB 规范的适当部分

最佳答案

根据 EJB 3.2 规范的第 7.1 节:

It is illegal to associate JTA transactional interceptors (see [8]) with Enterprise JavaBeans. The EJB Container should fail deployment of such applications.[39]

[39] This restriction may be removed in a future release of this specification.

由于 @Transactional 注释被错误地用于指定 EJB 上的 JTA 事务拦截器,因此 @ApplicationException 注释不起作用。尝试改用 @TransactionAttribute 注释。

关于java - 运行时异常是@ApplicationException(rollback=false) 但事务最终回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30246592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com