0
点赞
收藏
分享

微信扫一扫

Transactional注解不生效案例


源码下载

​​ChaiRongD/Demooo - Gitee.com​​

不生效场景1:try-catch捕获了

不生效原因:内部逻辑把异常捕获吞了,所以不生效

@Transactional
public void transactionalNotEffect01() {
try {
Person p1 = new Person();
p1.setName(LocalDateTime.now().toString());
personDao.insertSelective(p1);
int i = 1 / 0;
} catch (Exception e) {
e.printStackTrace();
System.out.println("my error");
}
}

不生效场景2:方法不使用public修饰

不生效原因:他会判断是不是public,源码中明确规定

Transactional注解不生效案例_java

@Service
public class StudentService {

@Autowired private PersonDao personDao;


// 不生效2
@Transactional
protected void transactionalNotEffect02() {
Person p1 = new Person();
p1.setName(LocalDateTime.now().toString());
personDao.insertSelective(p1);
int i = 1 / 0;
}

}

@RestController
public class TransactionalNotEffect02Controller {

@Autowired private StudentService studentService;

@GetMapping("/transactionalNotEffect02")
public Object getAll(){
studentService.transactionalNotEffect02();
return 1;
}
}

 不生效场景3:通过this调用,没有走代理方法

不生效原因:自己掉自己的方法,这样方式不会走代理,所以事务失效

 // 不生效3
public void transactionalNotEffect03() {
this.addPerson();
}

@Transactional
public void addPerson() {
Person p1 = new Person();
p1.setName(LocalDateTime.now().toString());
personDao.insertSelective(p1);
int i = 1 / 0;
}

@RestController
public class StudentController {

@Autowired private StudentService studentService;

@GetMapping("/transactionalNotEffect03")
public Object transactionalNotEffect03() throws Exception {
studentService.transactionalNotEffect03();
return 1;
}
}

不生效场景4:@Transactional 注解属性 propagation 设置错误

不生效原因:见下面的

参考:​​Spring中propagation的7种事务配置_sayoko06的博客-_propagation​​

@Service
public class StudentService {

@Autowired private PersonDao personDao;

// 不生效4
@Transactional(propagation= Propagation.NOT_SUPPORTED)
public void transactionalNotEffect04() {
Person p1 = new Person();
p1.setName(LocalDateTime.now().toString());
personDao.insertSelective(p1);
int i = 1 / 0;
}
}

@GetMapping("/transactionalNotEffect04")
public Object transactionalNotEffect04() throws Exception {
studentService.transactionalNotEffect04();
return 1;
}

不生效场景5:@Transactional的rollbackFor 属性与实际异常不匹配

根本原因:rollbackFor属性监听的是RuntimeException,而下面的代码是FileNotFoundException。不是父子关系,没有交集,匹配失败,所以事务失效。

@Transactional(propagation = Propagation.REQUIRED,rollbackFor = RuntimeException.class)
public void transactionalNotEffect05() throws Exception{
Person p1 = new Person();
p1.setName(LocalDateTime.now().toString());
personDao.insertSelective(p1);
//不存在的文件
int read = new FileInputStream(new File("D://尚硅谷周阳老师//1.txt")).read();
}

不生效场景6:数据库引擎不支持事务

不生效场景7:线程不同不支持事务

不生效原因:事务回滚的单位是建立在connection上,而connection是存在ThreadLocal中,因为不同的线程ThreadLocal存的connection自然不一样,那该场景就不符合预期(都回滚)了。

// 不生效7
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public void transactionalNotEffect07() {
Person p1 = new Person();
p1.setName(LocalDateTime.now().toString());
personDao.insertSelective(p1);
new Thread(
() -> {
Person p2 = new Person();
p2.setName(LocalDateTime.now().toString());
personDao.insertSelective(p2);
int i = 1 / 0;
},
"t1")
.start();
}

参考

1 尚硅谷周阳老师分享

​​2 Spring中propagation的7种事务配置_sayoko06的博客_propagation​​

​​3 @Transactional注解不起作用解决办法及原理分析_嘎嘎的博客_@transactional 不生效​​


举报

相关推荐

0 条评论