背景
我们知道update库表里的一条数据会产生行锁(以mysql innodb为例),提交事务后才会释放行锁,在行锁阶段别的事务无法去修改同一条记录。
在 Navicat 和命令行中,执行完这条update的 SQL 就立即提交事务。但是在Java程序里,用 @Transactional 修饰的方法只有执行完毕后才会提交事务。
除了update 语句,select for update 语句也是可以产生行锁的(也不一定是行锁,也可能表锁),可以用于临时锁定一行记录不被修改。
今天研究的课题就是研究下 select for update 的细节
代码直接上代码
- Rest 接口
@RestController
public class TestRest {
@Autowired
@Qualifier("testService1Impl")
private TestService testService1;
@Autowired
@Qualifier("testService2Impl")
private TestService testService2;
@Autowired
@Qualifier("testService3Impl")
private TestService testService3;
@Autowired
@Qualifier("testService4Impl")
private TestService testService4;
@GetMapping("/testSelectForUpdate1")
public Object testSelectForUpdate1(@RequestParam Integer id) {
return testService1.selectForUpdate(id);
}
@GetMapping("/update1")
public String update1(@RequestParam Integer id) {
String remark = new SimpleDateFormat("HH:mm:ss.SSS").format(new Date());
return "affected:" + testService1.update(id, remark);
}
@GetMapping("/testSelectForUpdate2")
public Object testSelectForUpdate2(@RequestParam Integer id) {
return testService2.selectForUpdate(id);
}
@GetMapping("/update2")
public String update2(@RequestParam Integer id) {
String remark = new SimpleDateFormat("HH:mm:ss.SSS").format(new Date());
return "affected:" + testService2.update(id, remark);
}
@GetMapping("/testSelectForUpdate3")
public Object testSelectForUpdate3(@RequestParam Integer id) {
return testService3.selectForUpdate(id);
}
@GetMapping("/update3")
public String update3(@RequestParam Integer id) {
String remark = new SimpleDateFormat("HH:mm:ss.SSS").format(new Date());
return "affected:" + testService3.update(id, remark);
}
@GetMapping("/testSelectForUpdate4")
public Object testSelectForUpdate4(@RequestParam Integer id) {
return testService4.selectForUpdate(id);
}
@GetMapping("/update4")
public String update4(@RequestParam Integer id) {
String remark = new SimpleDateFormat("HH:mm:ss.SSS").format(new Date());
return "affected:" + testService4.update(id, remark);
}
}
- Service实现类
其他 3 个Service实现类分别是:方法都去掉 @Transactional,去掉其中一个方法的 @Transactional
@Service
public class TestService1Impl implements TestService {
@Autowired
private TestMapper testMapper;
@Transactional
@Override
public MyTable selectForUpdate(int id) {
MyTable myTable = testMapper.selectForUpdate(id);
int totalSecs = 20;
for (int i = 0; i
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?