当前位置:网站首页>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
边栏推荐
- Deployment of external server area and dual machine hot standby of firewall Foundation
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- Acdreamoj1110 (multiple backpacks)
- Web开发小妙招:巧用ThreadLocal规避层层传值
- WEB功能测试说明
- OSPF multi zone configuration
- Is it profitable to host an Olympic Games?
- ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
- Nodejs教程之让我们用 typescript 创建你的第一个 expressjs 应用程序
- SDL2来源分析7:演出(SDL_RenderPresent())
猜你喜欢

监控界的最强王者,没有之一!

【力扣刷题】一维动态规划记录(53零钱兑换、300最长递增子序列、53最大子数组和)

LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件

Why do job hopping take more than promotion?

爱可可AI前沿推介(7.6)

2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history

数据湖(八):Iceberg数据存储格式

3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
![[MySQL] basic use of cursor](/img/cc/39b1e17b48d0de641d3cbffbf2335a.png)
[MySQL] basic use of cursor

【Redis设计与实现】第一部分 :Redis数据结构和对象 总结
随机推荐
基于深度学习的参考帧生成
Web开发小妙招:巧用ThreadLocal规避层层传值
SQL:存储过程和触发器~笔记
JS操作dom元素(一)——获取DOM节点的六种方式
2017 8th Blue Bridge Cup group a provincial tournament
El table table - sortable sorting & disordered sorting when decimal and% appear
Why do job hopping take more than promotion?
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Aike AI frontier promotion (7.6)
Comparison between multithreaded CAS and synchronized
[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
Is it profitable to host an Olympic Games?
967- letter combination of telephone number
C语言:#if、#def和#ifndef综合应用
Aiko ai Frontier promotion (7.6)
JS according to the Chinese Alphabet (province) or according to the English alphabet - Za sort &az sort
Swagger UI教程 API 文档神器
启动嵌入式间:资源有限的系统启动
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
[Li Kou brush questions] 32 Longest valid bracket