EJB 3 Transaction Handling and Rollback
843830Feb 13 2009 — edited Feb 17 2009I have a question regarding how EJB container manages rollback in case of exceptions. I have an application specific Exception class which is marked for rollback using EJB3 specification
@ApplicationException(rollback = true)
This works well in most of the cases where I have created a new Transaction, or have joined an existing one. But in cases where I do not require a new Transaction, especially in read-only conditions, but I require to throw application exception due to business requirement, this doesn't work. The EJB container throws an IllegalArgumentException because it tries to rollback a Transaction which is not existing at all. The work around for this would be to throw another type of application exception which is not marked for rollback. I do not know wheter this would be a good solution.
Is this the inherent behaviour of the container? I think the EJB container should be clever enough to detect whether there is always a transaction before doing a rollback. EJB3 is supposed to reduce the work of the programmers, and hence came upon the idea of using annotations. But I do not know if this is a potential bug in EJB3 or if I am wrong somewhere.
Any comments regarding this would be greatly appreciated.