当前位置:网站首页>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 .
边栏推荐
- Three updates to build applications for different types of devices | 2022 i/o key review
- How can I apply for a PMP certificate?
- Self awakening from a 30-year-old female programmer
- Run can start normally, and debug doesn't start or report an error, which seems to be stuck
- Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute
- Chaosblade: introduction to chaos Engineering (I)
- [istio introduction, architecture, components]
- C language pointer (special article)
- 外部中断实现按键实验
- 寄存器地址名映射
猜你喜欢
Variable parameter of variable length function
嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数
STM32 and motor development (from stand-alone version to Networking)
Sublime Text4 download the view in bower and set the shortcut key
JVM garbage collection detailed learning notes (II)
Register address name mapping
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
PMP Exam details after the release of the new exam outline
Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute
Hard core sharing: a common toolkit for hardware engineers
随机推荐
Test Engineer Interview Questions 2022
How can I apply for a PMP certificate?
浏览器中如何让视频倍速播放
Leetcode daily questions (2316. count unreachable pairs of nodes in an undirected graph)
C语言指针(习题篇)
JWT certification used in DRF
What is the value of getting a PMP certificate?
Why is access to the external network prohibited for internal services of the company?
Count the number of words in the string c language
The use of recycling ideas
Mysql数据库-锁-学习笔记
[istio introduction, architecture, components]
5A summary: seven stages of PMP learning
端口复用和重映像
Jenkins+ant+jmeter use
串口實驗——簡單數據收發
十二、排序
Locust performance test 5 (analysis)
PMP certificate preparation experience sharing
Postman setting environment variables