当前位置:网站首页>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 .
边栏推荐
- C语言指针(特别篇)
- 十二、排序
- Simulation volume leetcode [general] 1705 The maximum number of apples to eat
- Integer or int? How to select data types for entity classes in ORM
- Chaosblade: introduction to chaos Engineering (I)
- Data association between two interfaces of postman
- Some pit avoidance guidelines for using Huawei ECS
- Port occupation troubleshooting
- H3C vxlan configuration
- Pytest installation (command line installation)
猜你喜欢

Druid monitoring - Introduction to JMX usage and principle

What is the use of PMP certificate?

Postman interface debugging method

2022-06-30 unity core 8 - model import

Register address name mapping

Mysql database index study notes

2022-07-06 unity core 9 - 3D animation

C language pointer (Part 1)

信息安全实验四:Ip包监视程序实现

MySql数据库-索引-学习笔记
随机推荐
Yapi test plug-in -- cross request
Jenkins+ant+jmeter use
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]
超十万字_超详细SSM整合实践_手动实现权限管理
Entity of cesium data visualization (Part 1)
Count the number of words C language
Postman interface debugging method
C language pointer (Part 1)
十二、排序
Three updates to build applications for different types of devices | 2022 i/o key review
Record of structured interview
Cmake command line use
What are the conditions for applying for NPDP?
When inputting an expression in the input box, an error is reported: incorrect string value:'\xf0\x9f... ' for column 'XXX' at row 1
[cloud native] Devops (I): introduction to Devops and use of code tool
Locust performance test 5 (analysis)
2020 year end summary
【云原生】DevOps(一):DevOps介绍及Code工具使用
PMP examination experience sharing
C language pointer (special article)