当前位置:网站首页>Isolation level
Isolation level
2022-07-27 12:42:00 【Rippling rippling】
problem
When multiple transactions in the same application or multiple transactions in different applications are executed concurrently on the same dataset , There may be many unexpected problems , These problems can be divided into the following three types :
Dirty reading (Drity Read):
Two transactions are known A and B, A Read has been B Updated but not yet submitted data , after ,B Roll back the transaction ,A The data read is dirty data .
scene :
The company paid , Leaders 5000 Yuan to Tom On your account , But the transaction was not committed , and Tom Just to check the account , Found that the salary has arrived , There are too many accounts 5000 element , I'm very glad to , But unfortunately , The leader found that Tom My salary is not right , yes 2000 element , So the transaction is rolled back quickly , After modifying the amount , Commit a transaction ,Tom When checking the account again, I found that there were only more accounts 2000 element ,Tom I'll have a good time , From then on, I was depressed , On the road of no return ……
analysis :
The above situation is dirty reading , Two concurrent transactions :“ Business B: Leaders give Tom salary ”、“ Business A:Tom Check salary account ”, Business A Read transaction B Data not yet submitted .
It can't be read repeatedly (Non-repeatable read):
Two transactions are known A and B,A Read the same data multiple times ,B stay A In the process of multiple reading, the data is modified and submitted , Lead to A When reading the same data multiple times , Inconsistent results , Example :
scene :
Tom Take the wage card to spend , Pay the bill at the cashier after drinking and eating , The waiter told him about this consumption 1000 element ,Tom Give the bank card to the waiter , The waiter inserts the bank card POS machine ,POS The machine reads that the card balance is 3000 element , It's just Tom Dawdling to enter the password , His wife hid her ears in a flash Tom Salary card 3000 Yuan transferred to his account and submitted the transaction , When Tom Enter the password and click “ confirm ” After button ,POS The machine has detected Tom I don't have any money for my salary card , Deduction failed ,Tom I wonder , It's clear that Cary has money , So I doubt POS There are ghosts , Fight with the little cashier sister ,300 After the round, he died because of his serious injury ,Tom My wife is in pain , unhappy , Since then, we have embarked on the road of no return …
analysis :
The above situation is non repeatable , Two concurrent transactions ,“ Business A:POS Machine deduction ”、“ Business B:Tom My wife transfers money online ”, Business A Read the data in advance , Business B Then the data is updated and the transaction is committed , And the business A When you read the data again and deduct , The data has changed .
scene :
Tom When you take a paycard to spend , once POS The machine reads the salary card information ( That is, the transaction begins ),Tom Even if the wife transfers money , stay Tom Enter the password and click “ confirm ” After button ,POS The machine has detected Tom The balance on the payroll card has not changed , Finally, the deduction is successful .
analysis :
The above situation is called repeated reading
Fantasy reading (Phantom Read):
Two transactions are known A and B,A Read data from a table , then B Some new data has been inserted into the table , Lead to A Read the same table again , There will be more lines , In short , A range of records are read successively in a transaction , But the number of records read each time is different , Call it phantom reading , Example
scene :
Tom My wife works in the banking department , She often checks through the bank's internal system Tom Salary card consumption records .2019 year 5 Some day of the month , She inquired Tom The total consumption of the salary card in the current month (select sum(amount) from record where card_id=‘6226090219290000’ and date_format(create_time,’%Y-%m’)=‘2019-05’) by 80 element ,Tom My wife was very surprised , assume “ My husband is so frugal , It's good to marry him !”, and Tom At this time, I was just outside and paid at the cashier , consumption 1000 element , That is, a new one is added 1000 Yuan of consumption records and submitted transactions , The wife immersed in happiness inquired Tom Salary card consumption details of the current month (select amount from record where card_id=‘6226090219290000’ and date_format(create_time,’%Y-%m’)=‘2019-05’) To find out , The result of the search turned out to be one 1000 Yuan consumption ,Tom My wife was instantly furious , Takeout ordered a large durian , The evening fell ,Tom Living in deep water , I only feel the pain of needle pricking in my knee …
analysis :
The above situation is called Unreal reading , Two concurrent transactions ,“ Business A: Get transaction B Records of consumption ”、“ Business B: Added a new consumption record ”, Business A Get transaction B There is one more piece of data in the consumption record .
scene :
Teachers' A perform SQL Statement to change the grades of all students in the database from specific scores to ABCDE Hierarchy , The SQL After the statement is executed, before the transaction is committed , Teachers' B Insert a record of specific scores and commit the transaction , Teachers' A The transaction commits and executes the query SQL sentence , At this time, the teacher A I found that there was another record that had not been changed , It's like an illusion .
Summary : It's easy to confuse unreal reading with unreal reading , Unrepeatable reading focuses on revision , Unreal reading focuses on adding or deleting . To solve the problem of non repeatable reading, we only need to lock the line that meets the condition , To solve unreal reading, we need to lock the watch .
Solution
According to the actual demand , By setting the transaction isolation level of the database, you can solve the dirty reads that occur when multiple transactions are concurrent 、 Non repetition and unreal reading , The database transaction isolation level is from low to high Read uncommitted、Read committed、Repeatable read and Serializable And so on . Databases are different , It supports different transaction isolation levels :MySQL The database supports the above four transaction isolation levels , The default is Repeatable read;Oracle Database support Read committed and Serializable Two transaction isolation levels , The default is Read committed.
1、Read uncommitted( Read uncommitted ): Dirty reading possible 、 No repeated reading or phantom reading .
2、Read committed( Read the submission ): Avoid dirty reading , But there may be unrepeatable reading and unreal reading . The default level of most databases is Read committed, such as Sql Server Database and Oracle database . Be careful : This isolation level will only lock the corresponding rows when writing data .
3、Repeatable read( Repeated reading ): It can avoid dirty reading and non repeatable reading , But there may be unreal reading . Be careful :①、 When the transaction isolation level is repeatable , If the search condition has an index ( Include primary key index ) When , The default lock mode is next-key lock ;②、 If the search criteria are not indexed , When updating data, the entire table will be locked . A gap is locked by a transaction , Other transactions cannot insert records in this gap , This can prevent unreal reading .
4、Serializable( serialize ): Avoid dirty reading 、 No repeated reading or phantom reading , But the concurrency is very low , Rarely used in general . Be careful : This isolation level will lock the whole table when reading and writing data .
summary :
Transaction isolation level Dirty reading It can't be read repeatedly Fantasy reading
Read uncommitted( Read uncommitted ) √ √ √
Read committed( Read the submission ) × √ √
Repeatable read( Repeated reading ) × × √
Serializable( serialize ) × × ×
explain :√ Indicates that there may be ,× It means that there will be no
Be careful : Higher isolation level , The more data integrity and consistency can be guaranteed , But the greater the impact on concurrent performance .
MySQL Transaction isolation level
see :MySQL Database support Read uncommitted、Read committed、Repeatable read and Serializable Four transaction isolation levels , The default is Repeatable read, You can view it through the following statement MySQL Database transaction isolation level :
select @@global.tx_isolation,@@tx_isolation;
modify :MySQL The modification of database transaction isolation level is divided into global modification and current session modify , The specific modification method is as follows :
1、 Global modification
①、 stay my.ini Add the following configuration at the end of the configuration file :
# The optional parameters are :READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE.
[mysqld]
transaction-isolation = READ-UNCOMMITTED
②、 restart MySQL service
2、 At present session modify , Sign in MySQL Execute the following command after the database :
set session transaction isolation level read uncommitted;
边栏推荐
猜你喜欢

(10) STM32 - systick tick timer

爱可可AI前沿推介(7.27)

分布式系统架构理论与组件

Wechat applet session holding

Self built personalized automatic quotation system to cope with changeable quotation mode

XXL job parameter transfer

Set接口

Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning

An overview of kernel compilation system

CVPR22 | 关系意识的图神经架构搜索
随机推荐
一款能模糊的地方都能模糊的测试工具——Wfuzz
P1876 turn on the light [getting started]
V. introduction of other objectives and general options
xshell7可以登录mysql虚拟机不能登陆mysql
Insert sort summary
分布式系统架构理论与组件
概述有名内部类与匿名内部类
Keil mdk5.37 and above can add AC5 (armcc) compiler by themselves
爬虫
flinksql从Oracle同步数据到Doris,一共50几个字段,Oracle表中3000多万条
Laboratory procedures and references of chloramphenicol acetate
MySQL common commands
为什么需要外键?
Set interface
关于 CMS 垃圾回收器,你真的懂了吗?
CEPH distributed storage performance tuning (6)
HDU1698_Just a Hook
Plus SBOM: assembly line BOM pbom
About typora's inability to log in after March 9, 2022 -- resolved
2021-3-19-byte-face value