I understood that when Transaction attribute is REQUIRED, the transaction continues in the existing Transaction and if its REQUIRES_NEW, the transaction starts in a new transaction suspendin the existing transaction.
But I don't find any concrete evidence for the same.
Below is the code snippet i tried
I expected following things, but never happened
1. When inner method completes successfully and exception is thrown in outer method, I expected the inner table will not be rolledback as it is in a new transaction. But both tables were rolled back.
2. When inner table throws error, data from inner table is rolledback but not sure why data fro outer table is also rolled back.
All this behaviour seems to be following ACID, but what is the difference between REQUIRED and REQUIRES_NEW. Ideally both seems to be doing something.
How are you running this code? (whats your main like)?
For @Transactional annotation to work you need have a TransactionProxy (created automatically) when you use <tx:annotation-driven>
However spring proxies don't work in nested call scenario's - if a method of the class calls another method in the same class the proxy gets by-passed.
Provide your spring config & your main, if you need more info ...
yep your code looks good - but where is the main? how do you run this code?
To try out transaction - here is a suggestion. Put myMethod2() in a different class say Test2. Do you now notice any difference in the transaction working between Propagation.REQUIRED and Propagation.REQUIRES_NEW
Do you know how Spring works when you add a @Transactional annotation?
Ans: It does so by creating a proxy class for the class which has annotated methods.
Do you know how Spring Proxy object works when one method in the proxied class calls another method in the same proxied class?
Ans: Sprig is not able to handle this scenario implicitly. Any annotation on the called method would be ignored (since the call happens on 'this' rather than on the Proxy)
You need to switch to AspectJ to handle such scenario's
If you really like to understand this behavior I recommend reading this section of the Spring documentation.