Call @Transactional annotated method from another @Transactional annotated method
It depends on the txType. By default it is REQUIRED. So the first method starts the transaction and the same transaction is used for the call to method2.
Also be careful that a method call inside the same object does not trigger the transaction processing. As typically the transaction handling is handled as proxy that only works when calling an injected other bean. In your example you would not notice a difference though.
A case where this would matter is if method1 is not @Transactional and method2 is. In this case there would be no transaction at all.
If both methods are in the same class, the @Transactional
annotation won't even be considered when calling from another method of the same class. It doesn't matter what you put there, or even if you leave it out. There still will be a transaction started by method1()
, but then you're stuck in that transaction.
If the second method were in a separate class, you could use Propagation.REQUIRES_NEW
to have it run in its own transaction, meaning that even if method1()
later on failed, any changes in method2()
would still have made.
The default transaction propagation of REQUIRED
starts a new transaction if none exist, or joins an existing transaction. Again, in the separate class situation it results in the rollback of any changes made in method2()
when method1()
fails.
Spring boot provides concept of propagation with @Transactions. Propagation level decides that inner transaction should be part of same outer transaction or it should be different isolated one. By default propagation level is REQUIRED which means inner transaction will be part of same outer transaction, so if inner transaction fails whole transaction will get rollback.
Now its important to know that Rollback works for only Runtime exceptions by default. For checked Exceptions you have to specify that explicitly @Transcations(rollbackFor = Exception.class)
So to answer for your question is YES! It does rollback changes made by inner transaction.