当前位置:网站首页>MySQL - transaction details
MySQL - transaction details
2022-07-06 21:26:00 【Shuangchen legend Technology (Newton it College)】
One 、 Business (Transaction) Definition
Business : A smallest indivisible unit of work ; Usually a transaction corresponds to a complete business .
● A complete business needs batch DML(insert、update、delete) The statements are jointly completed .
● stay MySQL Only use Innodb The database or table of the database engine supports transactions .
● Transactions can be used to maintain database integrity , Make sure that there are lots of SQL Statement or all execution , Or none at all .
● Business is only with DML The sentence is about , Or say DML Statements have transactions . This has to do with business logic , Business logic is different ,DML The number of sentences is different
●MySQL Transactions are mainly used to handle large amount of operations , Data with high complexity . for instance , In the people management system , You delete a person , You need to delete the basic information of the personnel , Also delete information related to the person , Such as mailbox , Articles, etc , such , These database operation statements constitute a transaction !
Two 、 The four characteristics of affairs (ACID)
Generally speaking , Transactions must be satisfied 4 Conditions (ACID):Atomicity( Atomicity or indivisibility )、Consistency( Uniformity )、Isolation( Isolation or independence )、Durability( persistence )
● Atomicity (Atomicity): A set of transactions , Or success ; Or withdraw , That is, if an error occurs during the execution of a transaction, it will be rolled back to the state before the start of the transaction .
● Uniformity (Consistency) : A transaction, whether before or after the beginning , The integrity of the database has not been destroyed . Therefore, the written data must fully comply with all preset rules ( Data accuracy 、 Serialization and subsequent databases can spontaneously complete the scheduled work ).
● Isolation, (Isolation): The database allows multiple transactions to read, write and modify its data simultaneously , Isolation can prevent data inconsistency caused by cross execution when multiple transactions are executed concurrently . Transaction isolation can be divided into :Read uncommitted( Read uncommitted )、Read committed( Read the submission )、Repeatable read( Repeatable )、Serializable( Serialization ).
● persistence (Durability): The changes made to the data after the transaction is completed are permanent , Cannot lose
2.1 Isolation, (Isolation) The level of
Business A And transaction B There is isolation between
● Isolation has the following 4 Isolation levels :
1、 Read uncommitted :read uncommitted
● Business A And transaction B
● At this isolation level , All things can see the execution 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 submitted :read committed
● The default isolation level for most database systems ( But it's not MySQL default )
● Satisfies the simple definition of isolation : One thing is invisible to others before it is submitted , This isolation level also supports so-called non repeatable reads (Nonerepeatable Read), Because other instances of the same transaction may have new commit, So the same select May return different results .
3、 Repeatable :repeatable read
●MySQL Default isolation level
● Ensure that multiple instances of the same transaction read data concurrently , You will see the same data lines
● But this will lead to another thorny problem , Fantasy reading (Phantom Read)
● Unreal reading means that when a user reads a range of data rows , Another thing has inserted a new line in this range , When the user reads the data within this range again , You'll find something new " The phantom " That's ok ,Innodb and Falcon The storage engine is controlled by multi version concurrency (MVCC) The mechanism solved the problem
4、 Serialization :serializable
● Highest isolation level
● By forcing things to sort , Make it impossible for transactions to conflict with each other , Thus the problem of unreal reading is solved
● in short , It is to add a shared lock to each read data row
● At this level , It may lead to a large number of timeout phenomena and lock competition
2.2 The role of isolation
If there is no isolation , Several problems that will occur
● Dirty reading (Dirty Read)
One transaction reads data from another uncommitted transaction
● It can't be read repeatedly (NonRepeatable Read)
For a data in the database , Multiple queries within a transaction range return different data values , This is due to the interval between queries , Another transaction modifies and commits the data .
The difference between unrepeatable and dirty reading is that , A dirty read is when a transaction reads dirty data that is not committed by another transaction , A non-repeatable read reads the data committed by the previous transaction .
In some cases , Unrepeatable reading is not a problem , For example, we repeatedly query a certain data, of course, the results of the final query are mainly . But in other cases, problems can occur , For example, for the same data A and B The sequence of queries may be different ,A and B A fight might break out …….
● Fantasy reading (Phantom Read)
Data inserted by another transaction is read in one transaction , It leads to inconsistencies .
Business A Read data according to certain conditions , Period affairs B Insert new data with the same search criteria , Business A When reading again according to the original conditions , Found a transaction B Newly inserted data
3、 ... and 、 Terms about transactions
● Some terms related to transactions :
● Open transaction :Start Transaction
● End of transaction :End Transaction
● Commit transaction :Commit Transaction
● Roll back the transaction :Rollback Transaction
Four 、 How to start a transaction , What is the identification ? How the transaction ends , What is the identification ?
● Open the sign :
● To perform a DML sentence , Start the transaction .
●eg:insert into、update、delete
● End mark :
● Submit : The end of success , Will all DML Statement operation history is synchronized with the data of the underlying hard disk
● Roll back : The end of failure , Will all DML The statement operation history is cleared
4.1 Example : Commit of transaction
● Submit ( Transaction success )
mysql> select * from users;
±-----±-----+
| id | name |
±-----±-----+
| 1 | abc |
±-----±-----+
1 row in set (0.00 sec)
mysql> start transaction; # Start business
Query OK, 0 rows affected (0.00 sec)
mysql> insert into users values(2,‘test’); # perform DML sentence
Query OK, 1 row affected (0.00 sec)
mysql> commit; # Submit
Query OK, 0 rows affected (0.00 sec)
mysql> select * from users; #DML The data inserted by the statement already exists in the table
±-----±-----+
| id | name |
±-----±-----+
| 1 | abc |
| 2 | test |
±-----±-----+
2 rows in set (0.00 sec)
4.2 Example : Rollback of transaction
● Roll back ( Transaction failure )
mysql> select * from users;
±-----±-----+
| id | name |
±-----±-----+
| 1 | abc |
| 2 | test |
±-----±-----+
2 rows in set (0.00 sec)
mysql> start transaction; # Start business
Query OK, 0 rows affected (0.00 sec)
mysql> insert into users values(3,‘xx’); # perform DML sentence
Query OK, 1 row affected (0.00 sec)
mysql> rollback; # Roll back
Query OK, 0 rows affected (0.00 sec)
mysql> select * from users; #DML The data inserted by the statement is not written to the table
±-----±-----+
| id | name |
±-----±-----+
| 1 | abc |
| 2 | test |
±-----±-----+
2 rows in set (0.00 sec)
5、 ... and 、 Transaction control statement
1、 Start a transaction
●begin
or
●start transaction
2、 Set the savepoint , There can be multiple savepoints in a transaction
●savepoint Save point name
3、 Commit transaction , And make all changes made in the database permanent
●commit work
or
●commit
4、 Rollback ends the user's transaction , And undo all pending changes
●rollback work
or
●rollback
5、 Delete a transaction savepoint , If no save point is specified , Executing this statement will report an error
●release savepoint Save point name
6、 Roll the transaction back to the marked point
●rollback to Marker points
7、 Set the isolation level of the transaction
●set transaction isolation level[READ UNCOMMITTED|READ COMMITTED|REPEATABLE READ|SERIALIZABLE]
●InnoDB The isolation levels that the storage engine provides for transactions are READ UNCOMMITTED、READ COMMITTED、REPEATABLE READ and SERIALIZABLE
边栏推荐
- Dialogue with Jia Yangqing, vice president of Alibaba: pursuing a big model is not a bad thing
- Aiko ai Frontier promotion (7.6)
- Comparison between multithreaded CAS and synchronized
- OneNote in-depth evaluation: using resources, plug-ins, templates
- Start the embedded room: system startup with limited resources
- KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
- 在最长的距离二叉树结点
- [redis design and implementation] part I: summary of redis data structure and objects
- @Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
- @PathVariable
猜你喜欢
[redis design and implementation] part I: summary of redis data structure and objects
Aike AI frontier promotion (7.6)
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
【Redis设计与实现】第一部分 :Redis数据结构和对象 总结
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
[Li Kou brush questions] 32 Longest valid bracket
[MySQL] basic use of cursor
爱可可AI前沿推介(7.6)
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
随机推荐
Web开发小妙招:巧用ThreadLocal规避层层传值
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
Start the embedded room: system startup with limited resources
ACdreamoj1110(多重背包)
039. (2.8) thoughts in the ward
@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
防火墙基础之外网服务器区部署和双机热备
el-table表格——sortable排序 & 出现小数、%时排序错乱
c语言char, wchar_t, char16_t, char32_t和字符集的关系
中国白酒的5场大战
愛可可AI前沿推介(7.6)
In JS, string and array are converted to each other (II) -- the method of converting array into string
Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
js通过数组内容来获取数组下标
袁小林:安全不只是标准,更是沃尔沃不变的信仰和追求
Proxy and reverse proxy
Reference frame generation based on deep learning
[MySQL] basic use of cursor
document. Usage of write () - write text - modify style and position control
Fzu 1686 dragon mystery repeated coverage