当前位置:网站首页>Sql--- related transactions
Sql--- related transactions
2022-07-02 01:03:00 【Game programming】
The four characteristics of affairs

- Atomicity (Atomicity): Transactions are the smallest unit of execution , Division is not allowed . The atomicity of the transaction ensures that the action is either complete , Or it doesn't work at all ;
Uniformity (Consistency): After the execution of the transaction , The database changes from one correct state to another ;
Isolation, (Isolation): When accessing the database concurrently , One user's transaction is not interfered by other transactions , The database between concurrent transactions is independent ;
persistence (Durability): After a transaction is committed . Its changes to the data in the database are persistent , Even if the database fails, it should not have any impact .
What are the problems of concurrent transactions
Dirty reading
When a transaction is accessing data and modifying the data , This modification has not yet been committed to the database , At this time, another transaction also accesses the data , And then I used this data . Because this data is not submitted yet , So the data read by another transaction is “ Dirty data ”, basis “ Dirty data ” The operation may not be correct .
What if A The value of a row of data updated is A value , But did not submit , At this point, the transaction B I checked the value of this line of data , The value you see is A value , Then transaction B Get it A Value to do various operations , however A Right again A Value has been modified , Lead to B Did a lot of operations with the wrong value .
Missing changes
When a transaction reads a data , Another transaction also accesses the data , After modifying the data in the first transaction , The second transaction also modifies the data . In this way, the modification result in the first transaction will be lost
Business 1 Read data from a table A=20, Business 2 Also read A=20, Business 1 modify A=A-1, Business 2 Also modify A=A-1, final result A=19, Business 1 The modification of is lost .
It can't be read repeatedly
Read the same data multiple times within a transaction . Before the end of the business , Another transaction also accesses the data . that , Between two reads in the first transaction , Due to the modification of the second transaction, the data read by the first transaction twice may be different . This happens when the data read twice in a transaction is different , So it's called unrepeatable reading .
such as A Checking balance in , The first view of account balance is 300, here B Withdraw money from the account again 300,A The second time I checked the account, it was found that 0.
Fantasy reading
Unreal reading is similar to nonrepeatable reading . It happens in a transaction (T1) Read a few lines of data , Then another concurrent transaction (T2) When some data is inserted . In the subsequent query , The first thing (T1) There will be more records that don't exist , It's like an illusion , So it's called Unreal reading .
Simply speaking , You have a business A, Send a message first SQL sentence , There is a condition , To query a batch of data , Such as SELECT \* FROM table WHERE id > 10 . so what , It came out at the beginning 10 Data . Then this time , Other business B Several pieces of data were inserted into the table , And business B Also submitted , At this time, several lines of data are added .
Then business A At this point, the second query , Once again, as like as two peas before. SELECT \* FROM table WHERE id > 10 This article SQL sentence , Several pieces of data were inserted due to other transactions , This led to its query 12 Data .
Unreal reading is similar to nonrepeatable reading , Unreal reading is adding or missing a few lines of data , Unrepeatable reading refers to the modification of a single piece of data .
What are the transaction isolation levels ?MySQL The default isolation level for is ?
SQL The standard defines four isolation levels :
- READ-UNCOMMITTED( Read uncommitted ): Lowest isolation level , Allow read of uncommitted data changes , Can cause dirty reading 、 Phantom or unrepeatable reading .
READ-COMMITTED( Read committed ): Allow to read data submitted by concurrent transactions , Can prevent dirty reading , But phantom or unrepeatable reads can still occur .
REPEATABLE-READ( Repeatable ): Multiple reads of the same field are consistent , Unless the data is modified by the transaction itself , Can prevent dirty and unrepeatable read , But phantom reading can still happen .
SERIALIZABLE( Serializable ): Highest isolation level , Completely obey ACID Isolation level . All transactions are executed one by one , In this way, there is no interference between transactions , in other words , This level prevents dirty reads 、 Unrepeatable reading and phantom reading .

MySQL InnoDB Storage engine in REPEATABLE-READ( Can be reread ) The transaction isolation level uses Next-Key Lock Lock algorithm , So we can avoid the production of unreal reading , It works with other database systems ( Such as SQL Server) Is different . So InnoDB The default isolation level supported by the storage engine is REPEATABLE-READ( Can be reread ) The transaction isolation requirements can be fully guaranteed , That is to say SQL The standard SERIALIZABLE( Serializable ) Isolation level . But what you need to know is InnoDB The storage engine uses... By default REPEATABLE-READ( Can be reread ) There will be no loss of performance .
InnoDB There are three algorithms for storing engine locks :
Record lock: Locks on a single line record
Gap lock: Clearance lock , Lock a range , Not including the record itself
Next-key lock:record+gap Lock a range , Include the record itself Next-key lock That is, the data of this line and this piece , Are locked .
author : Garden baby Hutu
this paper [ SQL--- Related affairs ] Included in Game programming ️ - database , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome browser .
边栏推荐
- [eight sorts ①] insert sort (direct insert sort, Hill sort)
- PLC Analog input analog conversion FB s_ ITR (Mitsubishi FX3U)
- Weather forecast applet source code weather wechat applet source code
- Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)
- Global and Chinese markets of digital crosspoint switches and mux/demux 2022-2028: Research Report on technology, participants, trends, market size and share
- cookie、session、tooken
- To meet the needs of consumers in technological upgrading, Angel water purifier's competitive way of "value war"
- AIX存储管理之卷组属性的查看和修改(二)
- Minimize the error
- 【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)
猜你喜欢

Leetcode skimming: stack and queue 02 (realizing stack with queue)

Leetcode skimming: binary tree 01 (preorder traversal of binary tree)

Random avatar encyclopedia, multi category wechat applet source code with history_ Support traffic master

AIX存储管理之卷组的创建(一)

Common loss function of deep learning

使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)

工作中非常重要的测试策略,你大概没注意过吧
![[WesternCTF2018]shrine writeup](/img/26/1700095c9b38b9b74a1b1136e5d5de.jpg)
[WesternCTF2018]shrine writeup

excel数据透视表

测试人进阶技能:单元测试报告应用指南
随机推荐
King combat power query renamed toolbox applet source code - with traffic main incentive advertisement
The 8-year salary change of testers makes netizens envy it: you pay me one year's salary per month
Barbie q! How to analyze the new game app?
Global and Chinese markets for maritime services 2022-2028: Research Report on technology, participants, trends, market size and share
[leetcode] number of maximum consecutive ones
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
【会议资源】2022年第三届自动化科学与工程国际会议(JCASE 2022)
Leetcode skimming: stack and queue 03 (valid parentheses)
工作中非常重要的测试策略,你大概没注意过吧
Random avatar encyclopedia, multi category wechat applet source code with history_ Support traffic master
什么是商业养老保险?商业养老保险安全靠谱吗?
Cat Party (Easy Edition)
449 original code, complement code, inverse code
What does open loop and closed loop mean?
2022 low voltage electrician examination questions and answers
2022 high altitude installation, maintenance and removal of test question simulation test platform operation
Picture puzzle wechat applet source code_ Support multi template production and traffic master
RFID让固定资产盘点更快更准
Synthetic watermelon game wechat applet source code / wechat game applet source code
Datawhale 社区黑板报(第1期)