If I have the following code :
@Component
public class A{
@Transactional(propagation = Propagation.REQUIRED)
public void a(){
//logic
b();
//logic
}
@Transactional(propagation = Propagation.REQUIRED)
public void b(){
//logic
}
}
How many transactions open Spring in this code example?
From spring documentation: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html
It only creates one transaction.
Refering to the documentation
Propagation.REQUIRED
support a current transaction, create a new one if none exists. The answer to your question is :1 transaction, if there is no transaction when A#a() is called.
0- zero if there is already one because it will be reused.
It doesn't matter. When calling
b()
froma()
it won't be going through the proxy, so any transactional attributes onb()
won't be considered.The example code has 1 transaction open if
a()
orb()
is called through the proxy (i.e. outside of the class) and there isn't a transaction in progress already.I add to @pablo answer the notice that in your example you cannot see the actual difference because your call your method within the same object which make @transaction behavior on the second method transparent with no effect :