容器管理事务回滚

Container Managed Transaction RollBack

我有一个使用以下方法的永不过时的会话 bean:

 public void process(){

try {
       // function which inserts some data
        Properties properties = new Properties();
        InitialContext ic = new InitialContext(properties);
         CouponBatchSessionBeanRemote CBSBR = (CouponBatchSessionBeanRemote) ic.lookup(CouponBatchSessionBeanRemote.class.getName());
         CBSBR.createCouponBatchFromPlantAppFile(batch);

        } catch (Exception e1) {
            context.setRollbackOnly();

             try {
                Properties properties = new Properties();
                InitialContext ic = new InitialContext(properties);
                RPMRequestSessionBeanRemote RPMRq = (RPMRequestSessionBeanRemote)  ic.lookup(RPMRequestSessionBeanRemote.class.getName());
                RPMRq.updateRPMRqState(RPMRQID, "E");

                } catch (Exception e1) { }

        }

}

现在在第一个 try 块中,如果发生错误事务将被回滚。但是在调用 context.setrollbackonly() 之后,try 块事务不会被执行。 抛出的异常是事务回滚。

这是预期的行为。事务边界处于容器管理事务的方法级别。我假设是数据库插入的 writeLog 也将回滚。在这种情况下,您应该考虑使用 JTA,或者在单独的调用中编写数据库插入,作为发起调用的客户端异常处理的一部分。

EJB 1 (Requires){
    try{
      call EJB2 (requires new)
      //throw exception 
    }catch (handle EJB2 exception){
       writeLogInDB();
    }
}

由于整个当前事务都被标记为回滚,因此对 EJB 的第二次调用也将被回滚。您必须为您的 RPMRequestSessionBeanRemote bean 使用 @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED),具体取决于您是否还需要在那里进行交易。