当前位置:网站首页>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 节点下,依旧只能同时执行一个调度线程任务
多线程下,对同一个数据库操作,我们就可以在这个数据库
单独创建一个表,这个表当做分布式锁就可以了
边栏推荐
- Where can I download airserver? How to use tutorial
- 用FFT加速特殊矩阵的矩阵向量乘运算
- Part I basic information of the project
- idea一次启动多个项目
- 安全7.18作业
- Vision and intelligent learning recent journal reading and related knowledge learning
- Simulation of synchronization performance of BOC modulation and demodulation based on MATLAB, output tracking curve and identification curve under different lead lag code distance
- Simulation of voltage source PWM rectifier with double closed loop vector control based on Simulink
- 解决kotlin写Android项目编译报Execution failed for task ‘:app:kaptDebugKotlin‘.异常
- 494. Objectives and
猜你喜欢

智头条:智装论坛将于8月4日举行,2022全屋智能销售将破100亿

【Pygame实战】打扑克牌嘛?赢了输了?这款打牌游戏,竟让我废寝忘食。

Les raccourcis clavier liés à l'onglet ne peuvent pas être utilisés après la mise à jour du vscode

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

7.13WEB安全作业

MariaDB 数据库升级版本

Cloud native observability tracking technology in the eyes of Baidu engineers

Safety 7.18 operation

BGP federal experiment

152. Product maximum subarray
随机推荐
用FFT加速特殊矩阵的矩阵向量乘运算
centos7 中彻底卸载mysql
BGP联邦实验
Simulation of voltage source PWM rectifier with double closed loop vector control based on Simulink
[hiflow] regularly send Tencent cloud SMS sending group
STL deque
基于双目相机拍摄图像的深度信息提取和目标测距matlab仿真
Matlab simulation of solving multi-objective optimal value based on PSO optimization
After vscode is updated, the shortcut keys related to tab cannot be used
MapReduce InputFormat之FileInputFormat
[200 opencv routines] 225. Fourier descriptor for feature extraction
Jsd-2204 session management filter day19
152. Product maximum subarray
【机器学习基础】无监督学习(5)——生成模型
基于simulink的双闭环矢量控制的电压型PWM整流器仿真
【7.16】代码源 -【数组划分】【拆拆】【选数2】【最大公约数】
Part I basic information of the project
C# 计算某个字符在字符串中出现的次数
[machine learning basics] unsupervised learning (5) -- generation model
安全合理用电 收获清凉一“夏”