Search code examples
springspring-transactionstransactional

Spring's @Transactional on class level applies to what methods?


I have a class with several methods. Some are public and others aren't. See example below.

What methods will be executed in a transaction context? Only the public ones?

I couldn't find the answer in the documentation.

@Transactionl
public class A {
  public void pub() {...}
  void pack() {...}
  protected void prot() {...}
  private void pri() {...}
}

Solution

  • Yes, only public methods will exhibit the transactional settings. If you put @Transactional annotation on private, protected or package-visible methods or on the class which has such methods, there won't be any error raised, it's just that your transactional settings won't be executed for those methods.

    So in your case the transaction will be opened only on this method:

    public void pub() {...}
    

    The documentation could be found here.