当前位置:网站首页>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 :
边栏推荐
- 论文笔记: 极限多标签学习 GalaXC (暂存, 还没学完)
- 数据工程系列精讲(第四讲): Data-centric AI 之样本工程
- Easy to use js script
- Selenium element positioning (2)
- Gbase 8C database upgrade error
- Global and Chinese markets for single beam side scan sonar 2022-2028: Research Report on technology, participants, trends, market size and share
- HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
- Concept of storage engine
- [coppeliasim] efficient conveyor belt
- [solution] every time idea starts, it will build project
猜你喜欢
Pangolin Library: subgraph
Open source | Ctrip ticket BDD UI testing framework flybirds
[width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
500 lines of code to understand the principle of mecached cache client driver
PHP campus financial management system for computer graduation design
构建库函数的雏形——参照野火的手册
Card 4G industrial router charging pile intelligent cabinet private network video monitoring 4G to Ethernet to WiFi wired network speed test software and hardware customization
Exness: Mercedes Benz's profits exceed expectations, and it is predicted that there will be a supply chain shortage in 2022
Use image components to slide through photo albums and mobile phone photo album pages
随机推荐
leetcode-2.回文判断
RDD creation method of spark
MySQL learning notes - subquery exercise
安装php-zbarcode扩展时报错,不知道有没有哪位大神帮我解决一下呀 php 环境用的7.3
构建库函数的雏形——参照野火的手册
Use the list component to realize the drop-down list and address list
VIM usage guide
模板_求排列逆序对_基于归并排序
Social networking website for college students based on computer graduation design PHP
剑指 Offer 29. 顺时针打印矩阵
HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
Lecture 4 of Data Engineering Series: sample engineering of data centric AI
leetcode-2. Palindrome judgment
3D drawing ()
SPI communication protocol
Prepare for the autumn face-to-face test questions
【无标题】数据库中一条查询SQL执行的过程
【社区人物志】专访马龙伟:轮子不好用,那就自己造!
Compact lidar global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
MySQL lethal serial question 1 -- are you familiar with MySQL transactions?