Question

I have 3 Spring Components calling first->second->third. If I only have @Transactional annotation on first and third will transaction be propagated properly?

@Component public class C1 {
  @Autowired C2 c2;

  @Transactional public method1() {
    ...
    c2.method2();
  }
}

@Component public class C2 {
  @Autowired C3 c3;

  public method2() {
    ...
    c3.method3();
  }
}

@Component public class C3 {
  @Transactional public method3() {
    ...
  }
}
Was it helpful?

Solution

Yes, Transaction support is thread-bound. When method2() is executing, it does so in the same Thread and therefore has access to the current Transaction. The same goes for method3().

OTHER TIPS

@Transactional works as below

if its marked on any method then we can specify an attribute called propagation whose value could be Required by default or RequiredNew.

sample is @Transactional(readOnly = true,propagation=Propagation.REQUIRES_NEW) public void somemethod(){

anotherMethod();

}

if the attribute is just "Required" which is there by default then it maintains a single transaction for whole propogation of all methods no matter whether you declare @transactional in each method or not.

so all methods execution comes under same transaction and if any rollback happens in last method it affects til the method called.

if the attribute is set to "RequiredNew" then each method runs in their own tranactions so rollback in one method does not rollback other methods tranactions.

hope you are clear now

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top