今天代码评审,碰到了@Transactional 事务的分歧,我认为,只要方法被try catch了,并且没有做任何处理,就不会回滚事务,但事实并不是这样,因此我还跟同事打赌输了一支雪糕!!! 特此来csdn发个贴,这也是我都第一次发帖,哈哈~~~
直接上代码:
情况一:
Controller层代码:
@RestController
public class MyTestController {
@Autowired
private StudentCopyService studentCopyService;
@Autowired
private StudentTestService studentTestService;
@PostMapping("/test")
public void test() {
studentTestService.insert1();
}
}
service层代码:
@Resource
private StudentTestMapper studentTestMapper;
@Resource
private StudentCopyService studentCopyService;
@Transactional
public void insert1() {
StudentTest studentTest = new StudentTest();
studentTest.setId(99);
studentTest.setName("一定成功都");
studentTest.setCourse("试一试");
studentTest.setScore("试一试");
studentTestMapper.insert(studentTest);
try {
studentCopyService.insert2();
}catch (Exception e){
System.out.println("不做处理");
}
}
studentCopyService.insert2(); 这句代码是报错了,数据字段长度不够!代码如下:
@Resource
private StudentCopyMapper studentCopyMapper;
public void insert2() {
StudentCopy studentCopy = new StudentCopy();
studentCopy.setId(99);
studentCopy.setName("试一试长度太长啦太长啦太长啦太长啦太长啦"); //name得长度 超出了
studentCopy.setCourse("试一试");
studentCopy.setScore("试一试");
studentCopyMapper.insert(studentCopy);
}
这种情况insert2()方法,报错了,但是事务是不会回滚的。原因是insert1()方法 catch了 insert2()方法,并且没有做抛出异常处理。这样理解是没有错,我也一直这样认为的。但是。。。看下面解决方案!
解决方案:
1、在insert2()上 加上@Transactional 注解,这样也是可以回滚的。(因此我输掉了一支雪糕!!!!)
2、在insert1()方法里面catch 后在抛出一个异常,也是可以回滚的。
情况二:
Controller层代码:
@RestController
public class MyTestController {
@Autowired
private StudentCopyService studentCopyService;
@Autowired
private StudentTestService studentTestService;
@PostMapping("/test")
public void test() {
// studentTestService.insert1();
studentCopyService.insert1();
}
}
Service层代码:
@Service
public class StudentCopyService {
@Resource
private StudentCopyMapper studentCopyMapper;
@Transactional
public void insert1() {
StudentCopy studentCopy = new StudentCopy();
studentCopy.setId(99);
studentCopy.setName("1");
studentCopy.setCourse("试一试");
studentCopy.setScore("试一试");
studentCopyMapper.insert(studentCopy);
try{
insert2();
}catch (Exception e){
System.out.println("不做任务处理!!");
}
}
public void insert2() {
StudentCopy studentCopy = new StudentCopy();
studentCopy.setId(999);
studentCopy.setName("试试一试试一试超出长度啦超出长度啦超出长度啦");
studentCopy.setCourse("试一试");
studentCopy.setScore("试一试");
studentCopyMapper.insert(studentCopy);
}
}
情况三:
@Service
public class StudentCopyService {
@Resource
private StudentCopyMapper studentCopyMapper;
@Transactional
public void insert1() {
StudentCopy studentCopy = new StudentCopy();
studentCopy.setId(99);
studentCopy.setName("1");
studentCopy.setCourse("试一试");
studentCopy.setScore("试一试");
studentCopyMapper.insert(studentCopy);
try{
insert2();
}catch (Exception e){
System.out.println("不做任务处理!!");
}
}
@Transactional //这里加上注解,事务也不会全部回滚
public void insert2() {
StudentCopy studentCopy = new StudentCopy();
studentCopy.setId(999);
studentCopy.setName("试试一试试一试超出长度啦超出长度啦超出长度啦");
studentCopy.setCourse("试一试");
studentCopy.setScore("试一试");
studentCopyMapper.insert(studentCopy);
}
}
在同一个类里面,内部方法调用,并且try catch了insert2(),这样事务是不会全部回滚的。因为spring触发事务都机制是通过aop动态代理实现的。
硬是要这样写,也有解决方案: 在try catch里面在抛出一个异常就可以了!
新手第一次发帖子,有说的不对的地方,欢迎各位大佬指点一二!哈哈哈哈哈哈哈哈哈哈~~~~~~~~~么么哒~