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

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

Case1

@Transactional
public class UserServiceImpl implements UserService {

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


        
4条回答
  •  情话喂你
    2020-12-07 14:55

    Quoting from here

    The Spring team's recommendation is that you only annotate concrete classes with the @Transactional annotation, as opposed to annotating interfaces.

    Since this mechanism is based on proxies, only 'external' method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional!

提交回复
热议问题