当前位置:网站首页>select......for update 语句的功能是什么? 会锁表还是锁行?
select......for update 语句的功能是什么? 会锁表还是锁行?
2022-07-23 10:46:00 【一天不写代码难受】
1 语句意思
在项目代码里,看到
select * from xxl_job_lock where lock_name = 'schedule_lock' for update
以上的代码的意思是什么
select查询语句是不会加锁的,但是select …for update除了有查询的作用外,还会加锁呢,而且它是悲观锁。
那么它加的是行锁还是表锁,这就要看是不是用了索引/主键。
没用索引/主键的话就是表锁,否则就是是行锁。
2 思路
新建一个表,我们使用xxl-job 里面的表
他的这个表,这个字段是主键

我们首先是关闭自动提交
需要关闭自动提交,通过set @@autocommit=0; 设置为手动提交。0代表手动提交,1代表自动提交。
必须先关闭,不然语句一执行,就提交了,我们看不出我们要的结果
关闭之后,执行语句
select * from xxl_job_lock where lock_name = 'schedule_lock' for update
以上查询语句的意思是,不仅仅要查询,还要对这个sql语句进行加锁;一加锁之后,其他的线程要操作这个表,就被卡住了,要等到这个sql语句执行完成,其他线程对这个表的操作,才会执行,不然一直等,这样就实现了排它锁
我们就可以使用采用 select for update ,是排它锁。说白了 xxl-job 用一张数据库表来当分布式锁了,确保多个 xxl-job admin 节点下,依旧只能同时执行一个调度线程任务
多线程下,对同一个数据库操作,我们就可以在这个数据库
单独创建一个表,这个表当做分布式锁就可以了
边栏推荐
- Linked list review!
- Uniapp realizes horizontal click and slide menu
- 在一个有序数组中查找具体的某个数字(二分查找or折半查找)
- 【解决异常】Flink上传jar包至集群环境运行报未序列化异常
- Blazor quickly realizes Minesweeper
- Matlab simulation of depth information extraction and target ranging based on binocular camera images
- The current situation and history of it migrant workers
- Chapter 4 use%rest API classes create rest services
- 修改ssh命令行[[email protected]]#颜色
- Redis bloom filter
猜你喜欢

Six ways of uniapp route jump

Simulink simulation of ESP three-phase SVPWM controller

Matlab simulation of depth information extraction and target ranging based on binocular camera images

什么是服务器托管及和虚拟主机的区别

Full backpack!

JSD-2204-会话管理-过滤器-Day19

基于matlab的BOC调制解调的同步性能仿真,输出跟踪曲线以及不同超前滞后码距下的鉴别曲线

深入理解L1、L2正则化
[email protected]]#颜色"/>修改ssh命令行[[email protected]]#颜色

Exploration and practice of Ali multimodal knowledge atlas
随机推荐
STL map attribute
基于simulink的双闭环矢量控制的电压型PWM整流器仿真
Start process of activity
Skills to learn before going to primary school
修改ssh命令行[[email protected]]#颜色
安全7.18作业
MapReduce InputFormat之FileInputFormat
Xlswriter - Excel export
Idea five free plug-ins to improve efficiency
报错 | cannot read property ‘_normalized‘ of undefined
Matlab simulation of solving multi-objective optimal value based on PSO optimization
Clickhouse, let the query fly!!!
解决kotlin写Android项目编译报Execution failed for task ‘:app:kaptDebugKotlin‘.异常
Dynamic planning - force buckle
[turn] functional area division based on poi ()
Use of RSA encryption
MariaDB 数据库升级版本
The exclamation point of vscode +tab shortcut key cannot be used, and the solution to the problem of a-soul-live2d plug-in
安全作业7.22
Blazor quickly realizes Minesweeper