当前位置:网站首页>MySQL (IV) - transactions
MySQL (IV) - transactions
2022-07-06 02:18:00 【waldosia】
MySQL( Four )—— Business
1. Concept of transactions
A transaction is a mechanism 、 A sequence of operations , Contains a set of database operation commands , And all the commands as a whole to submit or revoke the operation request to the system , That is, all the database commands are executed , Either not .
A transaction is an indivisible unit of work logic , When performing concurrent operations on a database system , Transactions are the smallest unit of control .
Transaction is suitable for the scenario of multi-user operating database system at the same time , Like a bank 、 Insurance companies and securities trading systems, etc .
Transactions ensure the consistency of data through the integrity of transactions .
Transactions can improve reliability during updating and inserting information into tables .
To put it bluntly , Transaction , It's a sequence of operations , These operations are either performed , Either not , It is an indivisible unit of work .
2. The transaction ACID characteristic
ACID, In a reliable database management system (DBMS) in , Business (transaction) It should have four characteristics : Atomicity (Atomicity) , Uniformity ( consistency)、 Isolation, (Isolation)、 persistence (Durability). These are the characteristics of a reliable database .
Atomicity : A transaction is an indivisible unit of work , Either the operations in the transaction occur , Or none at all .
( A transaction is a complete operation , The elements of a transaction are indivisible . All elements in a transaction must be committed or rolled back as a whole . If any element in the transaction fails , Then the whole transaction will fail .)
Case study :
A to B transfer 100 Only the deduction statement was executed when the amount of RMB was RMB , Just submitted , If there is a sudden power failure ,A The account has been deducted ,B The account didn't receive the additional payment , It can cause disputes in life . In this case, the atomicity of transactions is needed to ensure that all transactions are executed , Or they don't do it .
Uniformity : Before and after the transaction , The database integrity constraint is not broken .
( When the transaction is complete , The data must be in a consistent state . Before the transaction starts , The data stored in the database is in a consistent state . In an ongoing transaction , The data may be in an inconsistent state . When the transaction completes successfully , The data must return to the known consistent state again .)
Case study :
For bank transfers , Whether the transaction succeeds or fails , You should make sure that after the transaction, the table A and B The total amount of deposit is the same as before .
Isolation, : In a concurrent environment , When different transactions manipulate the same data at the same time , Each transaction has its own full data space .
( All concurrent transactions that modify data are isolated from each other , Indicates that the transaction must be independent , It should not in any way depend on or affect other transactions . A transaction that modifies data can access that data before another transaction that uses the same data starts , Or access the data after another transaction using the same data ends . That is, when accessing the database concurrently , One user's transaction is not interfered by other transactions , The database between concurrent transactions is independent .)
// When multiple clients access the same table concurrently , The following consistency problems may occur :
(1) Dirty reading : One transaction reads uncommitted data from another transaction , And it's possible to roll back this data .
(2) It can't be read repeatedly : Two identical queries in a transaction return different data . This is caused by the commit of other transaction modifications in the system during query .
(3) Fantasy reading : A transaction modifies the data in a table , This modification involves all data rows in the table . meanwhile , Another transaction also modifies the data in this table , This modification is to insert a new row of data into the table . that , The user operating the previous transaction will find that there are no modified data rows in the table , It's like an illusion .
(4) Lost update : Two transactions read the same record at the same time ,A Change the record first ,B Also modify the record (B I do not know! A A modified ),B After submitting the data B The modification result covers A Modification results of .
// The isolation level of transactions determines the level of visibility between transactions .
MySQL Transaction supports the following four kinds of isolation , To control the changes made by the transaction , And notify the modification to other concurrent transactions :
(1)、 Uncommitted read (Read Uncommitted) :
Dirty reading allowed , That is, one transaction is allowed to see the uncommitted modifications of other transactions .
(2)、 Submit to read (Read Committed) :
A transaction is allowed to see only the changes committed by other transactions , Uncommitted changes are not visible .
(3)、 Repeatable (Repeated Read) : —mysql Default isolation level
Make sure that if you execute the same... Twice in a transaction SELECT sentence , We can get the same results , Whether or not other transactions commit these changes .
(4)、 Serial read (Serializable): — It's like a lock table
Read completely serially , Completely isolate one transaction from other transactions . Every time you read, you need to obtain a table level shared lock , Reading and writing will block each other .
mysql The default transaction level is repeatable read , and Oracle and SQL Server yes read committed .
// The scope of transaction isolation level is divided into two types :
- Global level : Valid for all conversations
- Session level : Valid only for the current session
Query global transaction isolation level :
Query session transaction isolation level :
Set the global transaction isolation level :
Set session transaction isolation level :
persistence (Durability): No matter whether the system fails or not , The result of the transaction is permanent ·— Once the transaction is committed , The effect of the transaction is permanently preserved in the database
persistence : After the transaction is completed , Changes made by the firm to the database are persisted in the database , It will not be rolled back . No matter whether the system fails or not , The result of the transaction is permanent .
( Once the transaction is committed , The effect of the transaction is permanently preserved in the database .)
summary : In transaction management , Atomicity is the foundation , Isolation is the means , Consistency is the purpose , Persistence is the result .
3. Transaction control statement
Transaction control statement
Transaction control statement | meaning |
---|---|
BEGIN or START TRANSACTION | Open a transaction explicitly |
COMMIT or COMMIT WORK | Commit transaction , And make all changes that have been made to the database permanent . |
ROLLBACK or ROLLBACK WORK | Rolling back ends the user's transaction , And undo all pending changes . |
SAVEPOINT S1 | Use SAVEPOINT Allows you to create a rollback point in a transaction , There can be more than one... In a transaction SAVBPOINT;"s1" Represents the name of the rollback point . |
ROLLBACK TO [SAVEPOINT] S1 | Roll back the transaction to the marked point . |
Case study :
4. Use set Set control transaction
Summary
The transaction ACID principle : Atomicity (Atomicity)、 Uniformity ( consistency)、 Isolation, (Isolation)、 persistence (Durability).Transaction control statement :
Transaction control method :
边栏推荐
猜你喜欢
Online reservation system of sports venues based on PHP
Computer graduation design PHP campus restaurant online ordering system
Lecture 4 of Data Engineering Series: sample engineering of data centric AI
[solution] every time idea starts, it will build project
Publish your own toolkit notes using NPM
Jisuanke - t2063_ Missile interception
同一个 SqlSession 中执行两条一模一样的SQL语句查询得到的 total 数量不一样
Prepare for the autumn face-to-face test questions
The intelligent material transmission system of the 6th National Games of the Blue Bridge Cup
Audio and video engineer YUV and RGB detailed explanation
随机推荐
Global and Chinese markets hitting traffic doors 2022-2028: Research Report on technology, participants, trends, market size and share
Publish your own toolkit notes using NPM
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
This time, thoroughly understand the deep copy
Paper notes: limit multi label learning galaxc (temporarily stored, not finished)
Minecraft 1.18.1, 1.18.2 module development 22 Sniper rifle
[depth first search] Ji Suan Ke: Betsy's trip
阿里测开面试题
【无标题】数据库中一条查询SQL执行的过程
Competition question 2022-6-26
Computer graduation design PHP animation information website
SQL statement
A basic lintcode MySQL database problem
零基础自学STM32-野火——GPIO复习篇——使用绝对地址操作GPIO
Unity learning notes -- 2D one-way platform production method
MySQL index
leetcode3、实现 strStr()
Executing two identical SQL statements in the same sqlsession will result in different total numbers
Know MySQL database
Lecture 4 of Data Engineering Series: sample engineering of data centric AI