前言
如果这是你第二次看到师长,说明你在觊觎我的美色!
点赞+关注再看,养成习惯
没别的意思,就是需要你的窥屏^_^
专车介绍
该趟专车是开往Spring Boot事务诡异事件的专车,主要来复现和分析事务的诡异事件。
专车问题
-
@Transaction标注的同步方法,在多线程访问情况下,为什么还会出现脏数据?
-
在service中通过this调用事务方法,为什么事务就不起效了?
专车示例
示例一
控制器代码
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService testService;
/**
* @param id
*/
@RequestMapping("/addStudentAge/{id}")
public void addStudentAge(@PathVariable(name = "id") Integer id){
for (int i = 0; i < 1000; i++) {
new Thread(() -> {
try {
testService.addStudentAge(id);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
}
service代码
@Service
public class TestService {
@Autowired
private StudentMapper studentMapper;
@Autowired
private TestService testService;
@Transactional(rollbackFor = Exception.class)
public synchronized void addStudentAge(Integer id) throws InterruptedException {
Student student = studentMapper.getStudentById(id);
studentMapper.updateStudentAgeById(student);
}
}
示例代码很简单,开启1000个线程调用service的