Вопрос

I suppose the following stacktrace as java invocation:

B.method2 (annotated with a plain @Transactional)
A.method1 (annotated with a plain @Transactional)
Main.main (starting point of the call, with no current transaction)

I expect that a transaction is started when A.method1 is entered - and the transaction will be commited (or rolled back) when A.method1 is left. I also expect that the same transaction will be used within B.method2.

A RuntimeException is thrown from within B.method2. This is a Exception that is 'listed' for rollbackFor by default. The Exception is catched within A.method1, but it will pass the boundary of @Transactional when leaving B.method2.

This is my question: Will the (current) transaction be marked for rollback or not?

Это было полезно?

Решение

The default propagation mode is REQUIRED and method2 will use transaction started for method1. On exception this transaction will be marked for rollback, so nothing will be committed to the database. You may get UnexpectedRollbackException after method1.

This is not a desired behavior since code which started the transaction (owns it) should be in control of rollback/commit. I would reorganize your code to avoid such possibility.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top