Avatar billede fredand Forsker
23. februar 2023 - 19:46 Der er 3 kommentarer

Will transaction be rolled back?

Hello,
Today I was investigating some old code that is run in a jee-application.
This is run in a container managed jee-server (weblogic).
I stumbled over this code (a bit simplified):

    public class AException extends Exception {
    }
   
    @Stateless
    @TransactionAttribute(REQUIRED)
    @TransactionTimeoutSeconds(1000)
    public class JobEJB implements Job{

        public void update() throws AException {
         
            boolean ok = true;
                try {
                    //do some update in a databse
                    for(int i = 0; i < 100; i++)
                   
                    if(i==50) {
                        XXXX
                    }
}
                }
                catch (AException  e) {
                    ok= false;
                    sessionContext.setRollBackOnly();
                    throw new AException();
                }
                catch (RuntimeException e) {
                    ok= false;
                    throw new AException();
                }
                finally {
                        System.out.println("Job: " + ok);
                }
        }
    }

I would say that if XXX above is replaced with:

    throw new AException();
   
...then the transaction will be rolled back since setRollBackOnly() is called.
   
    BUT if XXX below is replaced with:
   
    throw new RuntimeException();
   
...then the transaction will be NOT be rolled back, since the exception is catched and I guess the app server will not know about it, right?.

Am I right?

Best regards
Fredrik
Avatar billede arne_v Ekspert
23. februar 2023 - 20:28 #1
I would agree with you.

It doesn't work that way?

As I remember the rules, then system exceptions (RuntimeException and its subclasses including EJBException) cause rollback but application exception like the above AExeption does not cause rollback.
Avatar billede arne_v Ekspert
23. februar 2023 - 20:38 #2
BTW, the logic seems weird.

AException does not itself trigger rollback, but it does anyway by calling .setRollBackOnly().

RuntimeException does trigger rollback, but it is converted into an AException that does not.
Avatar billede arne_v Ekspert
23. februar 2023 - 20:40 #3
And exception wise I guess that:

              catch (AException  e) {
                    ...
                    throw new AException();
                }
                catch (RuntimeException e) {
                    ...
                    throw new AException();
                }

should be:

              catch (AException  e) {
                    ...
                    throw e;
                }
                catch (RuntimeException e) {
                    ...
                    throw new AException(e);
                }

To keep context for exceptions.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester