我有以下ejb:
for (int i = 1; i <= shopItem.getQuantity(); i++) {
purchase = new Purchase();
purchase.setUser(user);
// a lot of sets
purchase.setPhoneNumber(order.getPhoneNumber());
try {
financeEntityEjb.createPurchase(purchase);
} catch (NotEnoughFundsException e) {
throw new NotEnoughFundsExceptionWithRollback(e); // Making in rollable
}
}
public void createPurchase(Purchase purchase) throws InputValidationException, NotEnoughFundsException {
// a lot of calculations
em.persist(purchase);
em.flush();
/* Closing Order */
purchase.getOrder().setState(Order.State.PURCHASED);
em.merge(purchase.getOrder());
}我的例外课:
@ApplicationException(rollback = true)
public class NotEnoughFundsExceptionWithRollback extends NotEnoughFundsException {
public NotEnoughFundsExceptionWithRollback() {
}
public NotEnoughFundsExceptionWithRollback(Throwable e) {
super(e);
}
public NotEnoughFundsExceptionWithRollback(String message, Throwable e) {
super(message, e);
}
}因此,我有问题,ejb回滚所有的em.persist(购买),但忽略em.merge(purchase.getOrder());
循环在purchaseEjb中。CreatePurchase方法在financeEjb上
发布于 2016-07-21 21:28:37
我假设for-循环中的代码不在ejb或ejb中(您没有使用this)。在这种情况下,最有可能的问题是
EJB中的容器管理事务持续一个方法调用。根据事务属性,它会忽略、启动或加入事务。缺省值是必需的,这意味着事务从对createPurchase的调用开始,到方法完成时结束(如果这样,代理就会负责)。
事务在RuntimeException上回滚,当应用程序异常用@ApplicationException(rollback = true)抛出时,或者设置了rolledback标志时。
在您的示例中,很可能在createPurchase的事务上下文中没有任何一个发生。您没有显示NotEnoughFundsException的代码,但我假设它没有在@ApplicationException(rollback = true)中显示。
相反,您已经注释了NotEnoughFundsExceptionWithRollback,这是由调用EJB的客户机在事务上下文之外抛出的。
如果希望整个循环是原子操作,则需要将其放在事务上下文中(例如,使用EJB)。
https://stackoverflow.com/questions/38503772
复制相似问题