Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaEE - EJB 3.1: Can we force transaction rollback on checked exceptions?

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 ;)

like image 405
Olivier Masseau Avatar asked Nov 20 '13 08:11

Olivier Masseau


1 Answers

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.

like image 129
Gabriel Aramburu Avatar answered Oct 26 '22 07:10

Gabriel Aramburu