当前位置:网站首页>Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?
Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?
2022-07-27 05:00:00 【Meme_ xp】
1. What is a database transaction ?
A transaction is a set of atomic operations , These operations either all happen , Or it doesn't happen at all . Transactions transform a database from one consistency state to another .
If Xiaoming wants to transfer money to Xiaohong 1000 element , There are two key operations involved in this transfer : Reduce Xiao Ming's balance 1000 element , Increase the balance of Xiaohong 1000 element . In case of a sudden error between these two operations, such as the collapse of the banking system , As a result, Xiaoming's balance decreased while Xiaohong's balance did not increase , That's not right . Transaction is to ensure the success of both critical operations , Or they all have to fail .
2. How transactions are implemented
Transactions are based on redo log files (redo log) And rollback logs (undo log) Realized .
Every time a transaction is committed, all logs of the transaction must be written to the redo log file for persistence , The database can ensure the atomicity and persistence of transactions through redo logs .
Whenever there is a modification transaction , There will be undo log, If you need to roll back , According to undo log The reverse statement of , such as insert A record is delete A record .undo log It mainly realizes the consistency of the database .
Talk about redo log file (redo log) And rollback logs (undo log)
undo log Before the beginning of a transaction , Before operating on any data , First, back up the data to be operated to a place .redo log Any data operated in a transaction , Back up the latest data in one place .
The purpose of the transaction log : Instance or media failed , Transaction log files come in handy .
redo log
redo log Not written with the commit of the transaction , It's during the execution of the transaction , And start writing redo in . Specific drop strategies can be configured . Prevent at the point of failure , There are dirty pages not written to disk , It's restarting MySQL In service , according to redo log To redo , So as to achieve the feature of persistence of transaction's non disk data .RedoLog It's a product of the persistence of transactions .

undo log
undo log Used to roll back row records to a version . Before the transaction is committed ,Undo Saved version data before submission ,Undo The data in can be used as a snapshot of the old version of data for other concurrent transactions to read . It's a product of the atomicity of transactions , stay MySQL innodb Concurrent storage is used to implement version control engine .

Bold style
3. Transaction has four characteristics
1. Atomicity .
A transaction is the logical unit of work of a database , The operations contained in the transaction either do , Either not
2. Uniformity .
things The result of service execution must be to change the database from one consistency state to another . So when the database contains only the results of a successful transaction commit , Say the database is in a consistent state . If the database system Failure in operation , Some transactions are interrupted before they are completed , Some of the changes made to the database by these unfinished transactions have been written to the physical database , The database is in an incorrect state , Or rather, A state of inconsistency .
3. Isolation, .
The execution of a transaction cannot be interfered by other transactions . That is, within a transaction // The operation and data used are isolated from other concurrent transactions , Transactions that execute concurrently cannot interfere with each other .
4. Continuity .
Also called permanence , Once a transaction is committed , Its changes to the data in the database should be permanent . The next operation or failure should not have any effect on its execution result .
4.MySQL The four isolation levels
1.Read Uncommitted( Read uncommitted content )
At this isolation level , All transactions can see the results of other uncommitted transactions . This isolation level is rarely used in practical applications , Because its performance is not much better than other levels . Read uncommitted data , It's also called dirty reading (Dirty Read).
2.Read Committed( Read submissions )
This is the default isolation level for most database systems ( But it's not MySQL default ). It satisfies the simple definition of isolation : A transaction can only see changes that have been committed by the transaction . This level of isolation Also support the so-called Of It can't be read repeatedly (Nonrepeatable Read), Because other instances of the same transaction may have new commit, So the same select May return different results .
3.Repeatable Read( Can be reread )
This is a MySQL Default transaction isolation level for , It ensures that when multiple instances of the same transaction simultaneously read data , You will see the same data lines . But in theory , This can lead to another tricky problem : Fantasy reading (Phantom Read).
4.Serializable( Serializable )
By forcing transaction ordering , Make it impossible to conflict with each other , So as to solve the problem of unreal reading . in short , It is to add a shared lock to each read data line . At this level , It may lead to a large number of timeout phenomena and lock competition .

5. What is dirty reading ? Fantasy reading ? It can't be read repeatedly ?
1、 Dirty reading
Business A Read transaction B Updated data , then B Rollback operation , that A The data read is dirty 
2、 It can't be read repeatedly
Business A Read the same data multiple times , Business B In the transaction A During multiple reads , The data has been updated and submitted , Cause transaction A When reading the same data multiple times , result atypism .
3、 Fantasy reading
System administrator A Change the scores of all students in the database from specific scores to ABCDE Grade , But the system administrator B At this time, a specific score record was inserted , When the system administrator A After the change, I found that there is another record that hasn't been changed , It's like an illusion , This is called Unreal reading .
Fantasy reading , It's not that the result sets obtained by two reads are different , Unreal reading focuses on one aspect or another select The data state represented by the result of the operation cannot support the subsequent business operation . More specifically :select Whether a record exists , non-existent , Ready to insert this record , But enforcement insert Found that this record already exists , Can't insert , At this time, the unreal reading happened .
Unrepeatable reading focuses on revision , Unreal reading focuses on adding or deleting ( More or less lines ), A dirty read is a transaction rollback that affects another transaction .
边栏推荐
- The usage syntax and scene of selector, as well as the usage of background picture size, text box shadow and excessive effect
- C language address book management system (linked list, segmented storage of mobile phone numbers, TXT file access, complete source code)
- Digital integrated circuit: CMOS inverter (I) static characteristics
- 勤于奋聊聊现在还有哪些副业可以做
- SVN使用详解
- 报错:cannot read poperties of undefined(reading ‘then‘)
- 使用Photoshop出现提示“脚本错误-50出现一般Photoshop错误“
- 动态内存函数的介绍(malloc free calloc realloc)
- Cache read / write policies: cacheside, read/writethrough and writeback policies
- ps怎么导入lut预设?Photoshop导入lut调色预设教程
猜你喜欢

STL upper series - detailed explanation of list container

What about PS too laggy? A few steps to help you solve the problem

Explanation of index failure principle and its common situations

Structural mode - decorator mode

ps怎么导入lut预设?Photoshop导入lut调色预设教程

单元测试chapter6

Final Cut Pro中文教程 (1) 基础认识Final Cut Pro

Unit test Chapter6

【搜索】DFS之连通性模型 + 搜索顺序

网络协议详解:IP
随机推荐
网络协议详解:IP
【C语言】动态内存管理
vim的基本操作
How do I reset Photoshop preferences? PS method of resetting preferences
消防安全培训资料汇总
打开编程的大门
事件的接受与忽略
Digital integrated circuit: CMOS inverter (I) static characteristics
Final Cut Pro Chinese tutorial (1) basic understanding of Final Cut Pro
2019强网杯upload
"Photoshop2021 introductory tutorial" flatten "perspective images
TCP three handshakes and four disconnects
"Photoshop2021 tutorial" adjust the picture to different aspect ratio
Explanation of index failure principle and its common situations
js小技巧
使用Photoshop出现提示“脚本错误-50出现一般Photoshop错误“
Vscode opens a new chapter in the visualization of pull request update code branches
有趣的C语言
Idea 如何新建一个groovy的项目(图文详细解释)
深入 Qt5 信号槽新语法