Search code examples
javaspringtransactionspropagation

@Transactional(propagation = Propagation.REQUIRED) in spring?


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?


Solution

  • It doesn't matter. When calling b() from a() it won't be going through the proxy, so any transactional attributes on b() won't be considered.

    The example code has 1 transaction open if a() or b() is called through the proxy (i.e. outside of the class) and there isn't a transaction in progress already.