1

I thought that RuntimeExceptions in a @Transaction lead to a revert and checked exceptions not.

For example I have two classes:

A:

@POST
@Path( DO_SOMETHING )
@Consumes( MediaType.APPLICATION_JSON )
@Produces( { MediaType.APPLICATION_JSON } )
@Authenticated
public RestResponse doSomething( SetttingsDTO settings ) {
    Response eok = new Response();

    try {
        b.callMe( settings );

        eok.setResponse( "Successfull", null );
    } catch ( Exception ex ) {
        log.error( ex.getMessage() );
        eok.setResponse( ex );
    }

    return eok;
}

B class:

@Transactional
public void callMe( SetttingsDTO settings ) throws Exception {
    /*
    Here we do some things in the database
    */
    throw new IOException("Test");
}

In my understanding because the IOException is thrown and catched in the Class A the database should not return the database-changes. But in real the database does revert. Although the exception is catched my database-changes are written in the database.

Can someone please explain why?

Paul
  • 1,344
  • 4
  • 19
  • 37
  • 1
    Have you tried `@Transactional(rollbackOn = IOException.class)` ? – agoncal Feb 22 '21 at 09:18
  • @agoncal My database does rollback on every Exception, although it is catched. "rollbackOn = IOException.class) would not make a difference. – Paul Feb 22 '21 at 10:39

0 Answers0