0

I got this code. I am using spring boot and jpa

@Transactional

public class MyClass {

    public void createSomething() {
        try {
            saveAndFlush();
        } catch (Exception e) {
            // Since error has occured I want to insert this information
            // into a audit table

            repository.saveAndFlush();

        }
    }

}

Now when the exception happens, the transaction is rolled back and hence the error table insert is not happening.

I am seeing

HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null id in XXXXX entry (don't flush the Session after an exception occurs)

In order to fix this I tried to do the error insert in a new method marked with @Transactional(propagation = Propagation.REQUIRES_NEW) on the understanding that the inner transaction would complete and outer would roll back

But I don't see my intended output.

halfer
  • 19,824
  • 17
  • 99
  • 186
juniorbansal
  • 1,249
  • 8
  • 31
  • 51
  • Correct. It is indeed a duplicate – juniorbansal May 23 '16 at 16:05
  • @alan-hay - even after following the instructions in your original thread i am still seeing the same issue. I made it as transactional in individual methods and added a requires new propogation – juniorbansal May 23 '16 at 17:49
  • `REQUIRES_NEW` will only work if that method is called on another object. If you marked a method inside this class with `REQUIRES_NEW` and call it from within this class it won't work. This is due to the limitations of proxy based AOP. – M. Deinum May 26 '21 at 08:26

1 Answers1

1

Remove the @Transactional from class level, and use it on those methods which actually perform the transactions. (if you have more than 1 method in that class, of course).

If you want independent transactions to commit/rollback to database, then use REQUIRES_NEW propagation on the method which will not disturb the global transaction.

The default behavior of '@Transactional' without any propagation mentioned is, to join the global (calling) transaction if any available, and if not start a new transaction.

Since you have a global transaction, it is rolledback completely. Instead, you need independent transactions.

K139
  • 3,654
  • 13
  • 17