Nayan Wadekar
Nayan Wadekar

Reputation: 11622

Transaction across multiple EJBs

Recently refactoring some code, came across transaction rollback scenario where one EJB bean calls another. Now in exception block each bean has its context which is marked for rollback.

Is this a good practice or they should just re-throw the exception & finally the initiator bean only does the rollback.

Also, if there is single transaction spanned across EJB's, then rollback should happen at the originator bean or where it encountered exception.

Transaction type JTA for persistence with XA data source.

Upvotes: 0

Views: 929

Answers (1)

Leonardo
Leonardo

Reputation: 9857

With this

is marked for rollback

you mean that the EJB catch the exception and use setRollbackOnly ?

If that's the case, then it depends on your design to decide which approach is preferred.

Normally a transaction is rolled back if a system exception is detected by the container. Application exception on the opposite does not have this effect.

But, if your business logic require that even a business exception has an important impact such that it must have the effect of rolling back the whole transaction, then you have the choice of setRollBackOnly, or launch an application exception with rollback=true.

This second approach has also the effect of not destroying the bean.

Regarding your second question:

Also, if there is single transaction spanned across EJB's, then rollback should happen at the originator bean or where it encountered exception.

the rollback is managed by the container, and again it depends on your design. Keep in mind that the error may pass trough the hole of your unique bean in charge of rolling back the transaction, and not being catched at all. So you will end up with an unwanted scenario with a transaction not being rolled back at all.

Upvotes: 1

Related Questions