当前位置:网站首页>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 .
边栏推荐
- How can I apply for a PMP certificate?
- JVM 内存结构 详细学习笔记(一)
- What are the suggestions for PMP candidates?
- Mysql database transaction learning notes
- [chaosblade: delete pod according to the tag, pod domain name access exception scenario, pod file system i/o failure scenario]
- The use of recycling ideas
- 正则匹配以XXX开头的,XXX结束的
- 端口复用和重映像
- Serializer & modelserializer of DRF serialization and deserialization
- Skill review of test engineer before interview
猜你喜欢

Data association between two interfaces of postman

寄存器地址名映射

Implementation of corner badge of Youmeng message push

Ppt template and material download website (pure dry goods, recommended Collection)

stm32和电机开发(从单机版到网络化)

四、机器学习基础

PMP Exam details after the release of the new exam outline

【云原生】DevOps(一):DevOps介绍及Code工具使用

PMP examination experience sharing

MySQL master-slave delay solution
随机推荐
2021 year end summary
(3/8) method parameters of improper use of enumeration (2)
DRF authentication, permissions, and flow restrictions (only for views in DRF)
Three updates to build applications for different types of devices | 2022 i/o key review
C language pointer (Part 2)
PMP Exam details after the release of the new exam outline
Pycharm importing third-party libraries
On December 8th, 2020, the memory of marketing MRC application suddenly increased, resulting in system oom
Jemter operation
NVIC interrupt priority management
Skill review of test engineer before interview
C language pointer (special article)
SAP MM STO单据的外向交货单创建后新加ITEM?
C language pointer (exercises)
Several stages of PMP preparation study
How can I apply for a PMP certificate?
Jenkins modifies the system time
浏览器中如何让视频倍速播放
What is the use of PMP certificate?
What is the rating of Huishang futures company? Is it safe to open an account? I want to open an account, OK?