Search code examples
springspring-boottransactionsspring-transactions

How to create @Trasactional on Private method in Spring Proxy


Here is the code snippet.

public class A{

  public void m1(){
    //Do some stuff
    m2();
  }

 @Transactional
 private m2(){
   // Some DB operations
 }
}

In the above code, @Transactional is not working.

Is there a way where I can create @Transactional only on private method (not on public)?

Can someone please help.


Solution

  • There is no purpose of keeping @Transactional on private method, because this method eventually being called within the class itself. So proxy will never apply on that method.

    When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings.