Search code examples
javaspringtransactionscopespring-transactionstransactional

Spring: Should I have @Transactional annotation in between?


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() {
    ...
  }
}

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().