【发布时间】:2013-08-16 08:50:16
【问题描述】:
我在一个容器管理的事务 bean 中有这样的东西:
@PersistenceContext
private EntityManager em;
@Resource
private EJBContext ejbContext;
public void testTransaction() {
Model model1 = new Model();
em.persist(model1);
ejbContext.setRollbackOnly();
Model model2 = new Model();
em.persist(model2);//the line the problem
}
在最后一行(有问题)抛出 TransactionRequiredException:
javax.ejb.EJBException: javax.persistence.TransactionRequiredException: JBAS011469: Transaction is required to perform this operation (either use a transaction or extended persistence context)
但是在Mastering EJB 4th edition 书(搜索“注定的交易”或转到第 299 页)中是这样解释的,因为它不会引发任何此类异常,而是您应该只在资源之前检查ejbContext.getRollbackOnly() -饥饿的操作。
当然,在这个简单的示例中,我可以通过抛出带有 @ApplicationException(rollback=true) 注释的异常来避免该问题,但我只是想知道我错过了什么。
【问题讨论】:
标签: transactions ejb ejb-3.0 ejb-3.1