0
votes

I have a staleless session bean with following method:

 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) { }

        }

}

Now in first try block if an error occurs transaction is rolledback. However after calling context.setrollbackonly() following try block transaction is not executed. The Exception thrown is that of transaction rollback.

2
Is there an exception? - Gábor Bakos
The Exception is that of transaction rollback. - Kuldeep S Chauhan

2 Answers

0
votes

That is the expected behavior. A transaction boundary is at a method level for Container Managed Transactions. The writeLog which I assume is a db insert will also be rolled back. You should consider using JTA in this case or write the db insert in a separate call as part of exception handling by the client who initiates the call.

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

Since the whole current transaction is marked for rollback, the second call to EJB will also be rolled back. You have to use @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) or @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) for your RPMRequestSessionBeanRemote bean, depending whether or not you require transaction there also.