I know that if an EJB method throws a checked exception there is no transaction rollback.
But is there a way to force the rollback when there is a checked exception without having to call setRollbackOnly()
in each method or create my own Exception class annotated with @ApplicationException(rollback=true)
?
Currently all our EJB methods are throwing Exception (mymethod() throws Exception
) and we would like to apply a quick fix to have transaction rollback when this kind of exception is thrown without having to modify each method signature.
Is it possible by modifying a parameter on the application server (currently using Jboss AS 7.1.1), using an annotation on my EJB classes or by defining some kind of interceptor ?
Thanks for any help ;)
According to ejb 3.1 specification is also possible to specify the rollback attribute in the deployment descriptor file.
14.2.1 Application Exceptions
An application exception does not automatically result in marking the transaction for rollback unless the ApplicationException annotation is applied to the exception class and is specified with the rollback element value true or the application-exception deployment descriptor element for the exception specifies the rollback element as true.
Here you can check how to include the application-exception element in the ejb-jar.xml file. I think that it could looks like:
<assembly-descriptor>
<application-exception>
<exception-class>java.lang.Exception</exception-class>
<rollback>true</rollback>
</application-exception>
</assembly-descriptor>
I never tried this (and I'm not saying that this is good idea) but I think this should works since it is supported by specification.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With