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

1

7

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

Brynhild answered 20/11, 2013 at 8:47 Comment(0)
L
4

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.

Loanloanda answered 20/11, 2013 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.