I’m using an application with EJB and CMT transactions. I have an EJB (let´s call it as EJB1) with a CMT defined as “requires_new” by annotation, like:
@TransactionAttribute(TransactionAttributeType.REQUIRED)
Since there´s is no transaction until now, it creates one. Now, this EJB1 calls another EJB (EJB2). All I found in my researches is the unique way to create another transaction is using a “requires_new” like:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
It work perfectly, the new transaction is created. The problem is about the rollback situations: When EJB2 fails only it´s transaction is rolled back, as expected. When EJB1 fails all of them are rolled back, as expected too but not what I need…
What I need is similar, just this difference: when EJB1 fails it should be rolledback, but I want EJB2 still commited if it not fails.
Did I make me understandable? I don´t want a sub-transaction when it´s parent fails rolled back every one. I want a independent transaction (like a parallel one), for each EJB I invoke from the main EJB.
The transactional attributes you mentioned should work as you expected in your scenario; if the call to EJB2 succeeds it will commit on its own since it’s requires_new. In this case, though, if an untreated exception pops up before that EJB2 call, it might not even be invoked giving the impression that everything is being rolled back.
If that’s not the case, you can also control the transaction in EJB1 by injecting the EJBContext and calling context.setRollbackOnly(). This will mark the current transaction as to be rolled back when it finishes and it will work as you expect.