当前位置:网站首页>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 :
边栏推荐
- MySQL index
- 500 lines of code to understand the principle of mecached cache client driver
- 抓包整理外篇——————状态栏[ 四]
- Selenium element positioning (2)
- Derivation of Biot Savart law in College Physics
- Get the relevant information of ID card through PHP, get the zodiac, get the constellation, get the age, and get the gender
- I like Takeshi Kitano's words very much: although it's hard, I will still choose that kind of hot life
- 01. Go language introduction
- A basic lintcode MySQL database problem
- PHP campus financial management system for computer graduation design
猜你喜欢
leetcode3、實現 strStr()
2022年版图解网络PDF
Use image components to slide through photo albums and mobile phone photo album pages
Using SA token to solve websocket handshake authentication
[community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!
Unity learning notes -- 2D one-way platform production method
[solution] add multiple directories in different parts of the same word document
PAT甲级 1033 To Fill or Not to Fill
Lecture 4 of Data Engineering Series: sample engineering of data centric AI
剑指 Offer 30. 包含min函数的栈
随机推荐
Spark accumulator
Gbase 8C database upgrade error
Competition question 2022-6-26
PAT甲级 1033 To Fill or Not to Fill
genius-storage使用文档,一个浏览器缓存工具
Online reservation system of sports venues based on PHP
抓包整理外篇——————状态栏[ 四]
Global and Chinese markets hitting traffic doors 2022-2028: Research Report on technology, participants, trends, market size and share
2022 PMP project management examination agile knowledge points (8)
Computer graduation design PHP enterprise staff training management system
Multi function event recorder of the 5th National Games of the Blue Bridge Cup
Bigder:34/100 面试感觉挺好的,没有收到录取
Computer graduation design PHP animation information website
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
Regular expressions: examples (1)
SSM 程序集
Sword finger offer 12 Path in matrix
Xshell 7 Student Edition
更改对象属性的方法
leetcode-2.回文判断