@Transactional 注解失效的3种情况

发布时间 2023-04-10 17:04:41作者: 杨吃羊

第一种 Transactional注解标注方法修饰符为非public时,@Transactional注解将会不起作用

第二种 在类内部调用调用类内部@Transactional标注的方法。这种情况下也会导致事务不开启

 
@Component  
public class TestServiceImpl implements TestService {  
    @Resource  
    TestMapper testMapper;  
  
    @Transactional  
    public void fun2() {  
          
    }  
 
    public void fun1(){  
        //类内部调用@Transactional标注的方法。  
        fun2();  
    }  
  
}  

第三种 事务方法内部捕捉了异常,没有抛出新的异常,导致事务操作不会进行回滚

  
@Component  
public class TestServiceImpl implements TestService {  
    @Resource  
    TestMapper testMapper;  
  
    @Transactional  
    public void insertTestCatchException() {  
        try {  
            xxxx
//运行期间抛异常
throw new NeedToInterceptException("");

}catch (Exception e){ System.out.println(""); //catch捕获了Exception 异常,但没抛出异常
} } }