What is the difference between defining @Transactional on class vs method

后端 未结 4 520
-上瘾入骨i
-上瘾入骨i 2020-12-07 14:15

Case1

@Transactional
public class UserServiceImpl implements UserService {

    ...................
    public void method1(){
        try{
                    


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-07 14:57

    Suppose you have the following class:

    @Transactional(readOnly = true)
    public class DefaultFooService implements FooService {
    
      public Foo getFoo(String fooName) {
        // do something
      }
    
      // these settings have precedence for this method
      @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
      public void updateFoo(Foo foo) {
        // do something
      }
    }
    

    The @Transactional annotation on the class level will be applied to every method in the class.

    However, when a method is annotated with @Transactional (like, updateFoo(Foo foo)) this will take precedence over the transactional settings defined at the class level.

    More info:

    • Transaction management in Spring

提交回复
热议问题