私はコンテナ管理のトランザクション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 book (「Doomed Transactions」を検索するか、299 ページに移動) では、このような例外はスローされず、代わりejbContext.getRollbackOnly()
にリソースを大量に消費する操作の前にのみチェックする必要があるため、そのように説明されています。
もちろん、この単純な例では、注釈が付けられた Exception をスローすることで問題を回避できますが、@ApplicationException(rollback=true)
何が欠けているのだろうかと思います。