Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 988
Niharika Avatar asked Dec 11 '25 03:12

Niharika


1 Answers

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.

like image 138
K.D Avatar answered Dec 13 '25 20:12

K.D