运行时异常是@ApplicationException(rollback=false) 但事务最终被回滚
Runtime exception is @ApplicationException(rollback=false) but transaction is finally rolled back
我的运行时异常
@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 规范适当部分的清晰解释和 link
根据 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
注释。
我的运行时异常
@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 规范适当部分的清晰解释和 link
根据 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
注释。