当前位置:网站首页>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 .
边栏推荐
- JWT certification used in DRF
- Mysql database index study notes
- Chaosblade: introduction to chaos Engineering (I)
- Postman setting environment variables
- PMP certificate preparation experience sharing
- PMP examination experience sharing
- 5A summary: seven stages of PMP learning
- Integer or int? How to select data types for entity classes in ORM
- Implementation of corner badge of Youmeng message push
- Register address name mapping
猜你喜欢
PMP Exam details after the release of the new exam outline
How to pass the PMP Exam in a short time?
2020 year end summary
Entity of cesium data visualization (Part 1)
Where is the answer? action config/Interceptor/class/servlet
Full link voltage test of the e-commerce campaign Guide
Druid monitoring - Introduction to JMX usage and principle
What are the conditions for applying for NPDP?
外部中断实现按键实验
Mysql数据库-锁-学习笔记
随机推荐
STM32的时钟系统
Huawei hcip datacom core_ 03day
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
Port multiplexing and re imaging
Why is access to the external network prohibited for internal services of the company?
2022-07-06 unity core 9 - 3D animation
External interrupt to realize key experiment
JWT certification used in DRF
正则匹配以XXX开头的,XXX结束的
Connecting mobile phone with ADB
SAP MM STO单据的外向交货单创建后新加ITEM?
Chaosblade: introduction to chaos Engineering (I)
Sublime Text4 download the view in bower and set the shortcut key
华为HCIP-DATACOM-Core_03day
Simulation volume leetcode [general] 1706 Where does the ball meet
四、机器学习基础
5A summary: seven stages of PMP learning
[chaosblade: node disk filling, killing the specified process on the node, suspending the specified process on the node]
Skill review of test engineer before interview
Regular matching starts with XXX and ends with XXX