当前位置:网站首页>Mysql:select ... for update
Mysql:select ... for update
2022-07-07 09:21:00 【Wow, it's a small dish】
I met you these days select … for update Of sql sentence , Decided to sort it out mysql Two locking mechanisms of .
Mysql Database has two kinds of locks , One is shared lock , One is exclusive lock .
Shared lock ( Read the lock ,S lock )
select … from … lock in share mode
Multiple transactions share a lock , however Only read , Do not modify . When a transaction gets the shared lock and the lock is not released , Another transaction cannot modify the locked data .
The biggest feature of shared locks is that they can be shared , Multiple transactions can acquire locks and read data at the same time , also , When the lock is not released , Data cannot be modified , This can avoid the problems of dirty reading and non repeatable reading of the database .
Exclusive lock ( The mutex , Write lock ,X lock )
select … for update;
Exclusive lock Cannot be shared by multiple transactions , If a transaction acquires an exclusive lock of a row of data , Then other transactions cannot obtain the lock of this row of data , Including shared lock and exclusive lock . Transactions that obtain exclusive locks can read and modify data , After the transaction is committed , Release the lock .

demonstration
Create a table for testing
CREATE TABLE `emipe_user_entity` (
`id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT ' Primary key ',
`user_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' user name ',
`pass_word` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' User password ',
`status` int(11) DEFAULT NULL COMMENT ' User state ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
Use InnoDB Storage engine ,InnoDB Both row level locks are supported , It also supports table level locks , Row level locks are used by default
MySQL InnoDB The engine defaults to update,delete,insert The statement will automatically add an exclusive lock to the data involved .
Store data for the test form
INSERT INTO `emipe_user_entity` VALUES ('1', ' Little red riding hood ', '123456', 0);
INSERT INTO `emipe_user_entity` VALUES ('2', ' A passer-by ', '523456', 0);
Shared lock
Business A: Get shared lock , But don't commit the transaction
begin;
select * from emipe_user_entity where id = 1 lock in share mode;
Business B: Get shared lock , You can query the data
select * from emipe_user_entity where id = 1 lock in share mode;
Business C: Try modifying data with shared locks , Report errors
update emipe_user_entity set pass_word = '222222' where id = 1;
result :
Business C First, we will wait for the shared lock to be released , After the lock is released , You can modify the modified data , Due to transaction A The lock has not been released , After a long wait , Business C Throw the wrong :
Lock wait timeout exceeded;try restarting trasacrtion. Waiting for lock timeout ; Try to restart this transaction
Exclusive lock
Business A: Get the exclusive lock to query , The transaction does not commit
begin;
select * from emipe_user_entity where id = 1 for update;
Business B: Try to get an exclusive lock , Blocked
select * from emipe_user_entity where id = 1 for update;
Business C: Attempt to acquire the Shared lock , Blocked
select * from emipe_user_entity where id = 1 lock in share mode;
Business D: Without a lock , Can query data
select * from emipe_user_entity where id = 1;
-- The query is successful
Ordinary query statements do not have any locking mechanism .
边栏推荐
- Systick滴答定时器
- PMP Exam details after the release of the new exam outline
- Jmeters use
- LeetCode每日一题(2316. Count Unreachable Pairs of Nodes in an Undirected Graph)
- Run can start normally, and debug doesn't start or report an error, which seems to be stuck
- Druid monitoring - Introduction to JMX usage and principle
- 【SVN】SVN是什么?怎么使用?
- Unittest simple project
- Jenkins automated email
- stm32和电机开发(从单机版到网络化)
猜你喜欢
随机推荐
Jenkins task grouping
Simulation volume leetcode [general] 1705 The maximum number of apples to eat
Mysql数据库-锁-学习笔记
Postman setting environment variables
On December 8th, 2020, the memory of marketing MRC application suddenly increased, resulting in system oom
Simulation volume leetcode [general] 1706 Where does the ball meet
[cloud native] Devops (I): introduction to Devops and use of code tool
Several stages of PMP preparation study
What are the suggestions for PMP candidates?
Systick tick timer
C language pointer (Part 2)
【云原生】DevOps(一):DevOps介绍及Code工具使用
Ppt template and material download website (pure dry goods, recommended Collection)
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]
[istio introduction, architecture, components]
Summary of PMP learning materials
SiteMesh getting started example
How can I apply for a PMP certificate?
Interview question: general layout and wiring principles of high-speed PCB
串口實驗——簡單數據收發








