当前位置:网站首页>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 :
边栏推荐
- HDU_p1237_简单计算器_stack
- 更改对象属性的方法
- Computer graduation design PHP college student human resources job recruitment network
- MySQL learning notes - subquery exercise
- Campus second-hand transaction based on wechat applet
- VIM usage guide
- [depth first search notes] Abstract DFS
- 阿里测开面试题
- FTP server, ssh server (super brief)
- MySQL lethal serial question 1 -- are you familiar with MySQL transactions?
猜你喜欢
![Grabbing and sorting out external articles -- status bar [4]](/img/1e/2d44f36339ac796618cd571aca5556.png)
Grabbing and sorting out external articles -- status bar [4]

leetcode3、實現 strStr()

RDD partition rules of spark

使用npm发布自己开发的工具包笔记

How to generate rich text online

Minecraft 1.18.1, 1.18.2 module development 22 Sniper rifle
![[Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice](/img/3a/63f3e89ddf84f23f950ed9620b4405.jpg)
[Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice

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

Selenium waiting mode

Publish your own toolkit notes using NPM
随机推荐
数据工程系列精讲(第四讲): Data-centric AI 之样本工程
Have a look at this generation
Selenium element positioning (2)
MySQL learning notes - subquery exercise
Ali test open-ended questions
Blue Bridge Cup embedded_ STM32_ New project file_ Explain in detail
This time, thoroughly understand the deep copy
Open source | Ctrip ticket BDD UI testing framework flybirds
Use image components to slide through photo albums and mobile phone photo album pages
零基础自学STM32-野火——GPIO复习篇——使用绝对地址操作GPIO
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
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
leetcode3、實現 strStr()
Extracting key information from TrueType font files
Numpy array index slice
MySQL index
Paper notes: graph neural network gat
零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
Redis key operation
General process of machine learning training and parameter optimization (discussion)