当前位置:网站首页>MySQL default transaction isolation level and row lock
MySQL default transaction isolation level and row lock
2022-07-02 09:34:00 【Program source program】
Review of isolation level
The isolation level of database transactions mentioned above , In retrospect :
1、READ-UNCOMMITTED -> Read uncommitted : There is no insurance , Dirty reading 、 It can't be read repeatedly , Fantasy reading , It could happen
2、READ_COMMITTED -> Read the submission : Only submitted data can be read , No dirty reading
3、REPEATABLE_READ -> Repeatable : Dirty reading and unrepeatable reading will not occur , There may be unreal reading ,mysql non-existent ,Innodb The storage engine solves the problem of unreal reading through multi version concurrent control .
4、SERIALIZABLE -> Serialization : If serialization is performed , Line up one by one , There will be no data confusion and inconsistency , But the cost is low efficiency .
mysql innoDB Default isolation level -> REPEATABLE_READ: Repeatable
That is, the uncommitted data of other transactions will not be read during the current transaction , And the newly submitted data will not be read
verification
Let's verify , Verification step sorting :
1. Open two mysql link A and B( Not two execution windows ), link A and B Open a transaction respectively , The method is execution :BEGIN;
2. link A According to the ID Update a piece of data without execution COMMIT Submit operation , link B Query the data in , Verify whether there are dirty reads
3. link A According to the ID Update a piece of data to execute COMMIT Submit operation , link B Don't execute COMMIT When querying this data , Verify that there is no unreadable
4. New link C Insert a piece of data in , New link D Query the data , Verify whether there is unreal reading
The test table :
CREATE TABLE t_test_batch (t_id varchar(32) CHARACTER SET utf8 NOT NULL DEFAULT ‘’,code varchar(32) CHARACTER SET utf8 DEFAULT NULL COMMENT ‘ Code ’,name varchar(100) CHARACTER SET utf8 DEFAULT NULL COMMENT ‘ name ’,
PRIMARY KEY (t_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT=‘ Test the transaction isolation level ’;
Create a piece of data , Field name The value is 444:

Start validation
- Open a new link A And open the business , Perform modifications to the sample data

- Open a new link B And open the business , Perform a query on the sample data , Did not read link A Modification of data in transactions

link A In the implementation of COMMIT Commit transaction , Execute the link again B Query in , The return result does not change

link B In the implementation of COMMIT Commit transaction , Execute the link again B Query in , Return result changes , Read the link A Modification of transactions

5. link D Open transaction query code='AaA3’ data , No data . Link at this time C Insert code='AaA3’ The data of , link D Query again , Still can't find , link D Commit the current transaction , You can find the data by querying again . The screenshot here ignores ...
Conclusion
Conclusion :mysql The default transaction isolation level can ensure that dirty reads do not occur in transactions 、 No repeated reading or phantom reading . Business A Modified or added data , Business B Transactions can only be read after committing A Modification or addition of .
Let's go
Here's a question , If the transaction A and B Modify the same data at the same time , What will happen ? Continue to verify , Open two transactions , At the same time, execute the following update operation .
link A
BEGIN;
UPDATE t_test_batch t SET t.name=‘0.0.0’ WHERE t.t_id=‘0009c52726154074a650444a15f910ad’;
link B
BEGIN;
UPDATE t_test_batch t SET t.name=‘11111’ WHERE t.t_id=‘0009c52726154074a650444a15f910ad’;
Results found ,A It's no problem to execute first , Without submission ,B Stuck after execution , All the way around , It seems to be locked .

Check whether there is currently a lock (information_schema.INNODB_LOCKS), Found two records , Show 0009c52726154074a650444a15f910ad This record has two X lock , And the lock is ok .

S Lock and X lock
Mysql X lock : Exclusive lock - Write lock ,mysql Modifying data will lock the data to ensure data consistency , The transaction modification data will be locked automatically . Business A Lock a piece of data modification , The transaction B、 Business C To modify this data , stay A When no transaction is committed , You can only try to lock this data , Then queue up and fall into waiting . Exclusive lock , Exclusion does not allow other matters to interfere .
Mysql S lock : Said exclusive lock , It is necessary to mention the shared lock, that is S lock - Read the lock . To ensure that transactions read data , This data will not be modified by other transactions , The current transaction can add S lock , Multiple transactions can be locked at the same time , One layer at a time S lock . The purpose of this is : Now I just want to read data quietly , If you want to modify the data, wait until I'm finished !
meanwhile S Locks have another function , If someone is modifying this data , Currently, when reading this data, if you try to add a shared lock, you will also fall into waiting . Read without sharing lock , Adding a shared lock may wait !

When adding, deleting or modifying, it will automatically add X lock
Query is unlocked by default , You can add it manually S Lock and X lock
The way is
Add S lock : select * from table_xxx lock in share mode;
Add X lock : select * from table_xxx for update;
Optimistic lock and pessimistic lock
The above states mysql Two kinds of locks , Both locks are pessimistic , It means whether you believe it or not , Anyway, I think it's bound to conflict with others , I'll lock it first, I'll use it first .
There is also an optimistic lock , That is, do not lock directly , I'm optimistic that no one is competing with me at present , Only when submitting the modification can we judge whether there is a conflict by the version number . When it is found that the version number in the current data database is inconsistent with the currently used version number , Think the data has been modified , give up .
Pessimistic lock database has been implemented , Optimistic lock needs to be realized by ourselves .
remaining problems
The above lock , Lock type lock_mode You can see both of them record, Instant lock .
Another kind of lock is table lock . Baidu found that it was insert Lock the watch ,update Lock line , Or what goes through the primary key index lock line , Otherwise lock the watch , The actual test is not like this ..
Various attempts have not found how to appear for the time being information_schema.INNODB_LOCKS In the table lock_type For the wrong record, Want to have a watch lock , I haven't succeeded yet ~
However, you can manually lock the table :LOCK TABLES … WRITE/READ
Such as LOCK TABLES t_test_batch READ, During this period, all transaction execution, modification and addition will fall into wait .
Use UNLOCK TABLES Unlock , I don't know the difference between this and the concept of row lock and table lock , Here to learn more .
边栏推荐
- C语言之判断直角三角形
- Required request body is missing:(跨域问题)
- Micro service practice | introduction and practice of zuul, a micro service gateway
- How to use pyqt5 to make a sensitive word detection tool
- Difference between redis serialization genericjackson2jsonredisserializer and jackson2jsonredisserializer
- Matplotlib swordsman line - first acquaintance with Matplotlib
- Pool de connexion redis personnalisé
- FragmentTabHost实现房贷计算器界面
- Attributes of classfile
- Chrome browser tag management plug-in – onetab
猜你喜欢

How to use pyqt5 to make a sensitive word detection tool

Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method

Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedd

上班第一天的报错(AWVS卸载不彻底)

在SQL注入中,为什么union联合查询,id必须等于0

图像识别-数据增广

Chrome用户脚本管理器-Tampermonkey 油猴

Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别

西瓜书--第五章.神经网络

tinyxml2 读取和修改文件
随机推荐
AMQ 4043 solution for errors when using IBM MQ remote connection
C语言之到底是不是太胖了
Chrome video download Plug-in – video downloader for Chrome
Statistical learning methods - Chapter 5, decision tree model and learning (Part 1)
c语言编程题
定时线程池实现请求合并
双非本科生进大厂,而我还在底层默默地爬树(上)
图像识别-数据清洗
What are the differences between TP5 and laravel
微服务实战|Eureka注册中心及集群搭建
C语言之数据插入
西瓜书--第六章.支持向量机(SVM)
Supplier selection and prequalification of Oracle project management system
JVM指令助记符
BugkuCTF-web21(详细解题思路及步骤)
在SQL注入中,为什么union联合查询,id必须等于0
微服务实战|负载均衡组件及源码分析
Chrome browser tag management plug-in – onetab
Ora-12514 problem solving method
Matplotlib剑客行——布局指南与多图实现(更新)