0

I know that hibernate will rollback all the changes made inside a @Transactional annotated method if any exception occurs inside it.

I am also aware that if we handle the exception with try catch then hibernate won't perform transaction rollback operation.

My question is that will hibernate perform transaction rollback if I handle the exception with try catch from the caller method (The method which invoked the database update operation method annotated with @Transactional)??

T4puSD
  • 102
  • 10
  • 2
    Hibernate does not handle the transaction, the Transaction Manager does. – seenukarthi Dec 15 '21 at 08:52
  • Give it a try and you will find out. It is the best way to learn ;) – João Dias Dec 15 '21 at 11:40
  • 1
    @JoãoDias thanks for the suggestion! – T4puSD Dec 16 '21 at 14:42
  • 2
    Also be aware that Spring's @Transactional annotation will only cause rollback on a RuntimeException. Checked Exceptions will not cause a rollback. See https://stackoverflow.com/questions/7125837/why-does-transaction-roll-back-on-runtimeexception-but-not-sqlexception – GreyBeardedGeek Dec 16 '21 at 15:59

1 Answers1

1

Well after testing it out myself I found out that even if you handle an exception from the caller method the transaction will roll back.

If anyone wondering what will happen if the caller method itself has transactional annotation in it? It will throw this exception:

org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only

The reason for it is explained in this answer

T4puSD
  • 102
  • 10