当前位置:网站首页>MySQL - transaction
MySQL - transaction
2022-07-19 17:56:00 【Yan Zi】
Mysql—— Business
One 、 Overview of database transactions
Transaction is one of the important characteristics that distinguish database from file system , When we have transactions, we will keep the database Uniformity
, At the same time, we can also use the transaction mechanism Back to a point in time
, This ensures that the changes submitted to the database will not be lost due to system crash .
1、 Storage engine support
show engines Command to view the current MYSQL What are the supported storage engines , And whether these storage engines support transactions .
Can see in Mysql in , Only innoDB It's transactional .
2、 Basic concepts
Business : A set of logical operating units , It is the transition of data from one state to another .
Principles of transaction processing : Ensure that everything is done as A unit of work
To execute , There was an immediate failure , Can't change this way of execution . When multiple operations are performed in a transaction , Or all the affairs will be Submit
, So these changes are permanent
It's preserved ; Or the database management system will give up
All that has been done modify
, The entire transaction is rolled back to its original state .
3、 The transaction ACID characteristic
Atomicity (atomicity):
Atomicity means that a transaction is an indivisible unit of work , Or submit it all , Either all failures roll back . That is, the transfer is successful , Or transfer failed , There is no middle state . What if there's no guarantee of atomicity ? There will be data inconsistency ,A Account minus 100 element , and B Account increase 100 Meta operation failed , The system will be lost for no reason 100 element .Uniformity (consistency):
According to the definition , Consistency refers to before and after transaction execution , Data from aLegitimacy status
Change to anotherLegitimacy status
. This state issemantics
Not grammatical , Related to specific business .
What is the legal data state ? Satisfy Predetermined constraints
The state of is called legal state . More popular , This state is defined by you ( For example, meet the constraints in the real world ). Satisfy this state , The data is consistent , Not satisfied with this state , The data is inconsistent ! If an operation in a transaction fails , The system will automatically undo the currently executing transaction , Return to the state before the transaction operation .
give an example 1
:A The account has 200 element , Transfer accounts 300 Yuan goes out , here A The account balance is -100 element . You will naturally find that the data is inconsistent at this time , Why? ? Because you define a state , The balance column must >=0.
give an example 2
:A Account 200 element , Transfer accounts 50 Yuan to B Account ,A The money in the account is deducted , however B Account because of all kinds of accidents , The balance has not increased . You also know that the data is inconsistent at this time , Why? ? Because you define a state , requirement A+B The total balance of must remain unchanged .
give an example 3
: In the data sheet, we will full name
Field set to Uniqueness constraint
, At this time, when the transaction is committed or rolled back , If the name in the data sheet is not unique , It destroys the consistency requirements of transactions .
- Isolation, (isolation):
Transaction isolation means that the execution of one transaction cannot be interfered by other transactions , That is, the operations and data used within a transaction are isolated from other concurrent transactions , Transactions that execute concurrently cannot interfere with each other .
What happens if isolation cannot be guaranteed ? hypothesis A The account has 200 element ,B Account 0 element .A Account to B Account transfer twice , The amount of each time is 50 element , Execute in two transactions . If isolation cannot be guaranteed , The following situations will occur :
- persistence (durability):
Persistence means that once a transaction is committed , The change it makes to the data in the database isPermanence
Of , Other subsequent operations and database failures should not have any impact on it .
Persistence is through Transaction log
To ensure that the . Including the log Redo log
and Rollback log
. When we modify data through transactions , First, the change information of the database will be recorded in the redo log , Then modify the corresponding row in the database . The advantage of this is , Even if the database system crashes , After the database is restarted, you can also find the redo log that has not been updated to the database system , Re execution , This makes the transaction persistent .
4、 The state of the transaction
We know that transaction is an abstract concept , It actually corresponds to one or more database operations ,MySQL The transaction is roughly divided into several states according to the different stages of these operations :
- Activities of the (Active):
When the database operation corresponding to the transaction is in progress , Let's say the officeActivities of the
state . - Partially submitted (partially committed):
When the last operation in the transaction is completed , But because the operations are all performed in memory , The impact is notNo refresh to disk
when , Let's say the officePartial submission
The state of . - The failure of the (failed):
When the affairs officeActivities of the
perhapsPartially submitted
In the state of , You may have encountered some errors ( Error in the database itself 、 Operating system error or direct power failure, etc ) And cannot continue , Or artificially stop the execution of the current transaction , Let's say the officeThe failure of the
state . - Suspended (aborted):
If the transaction is partially executed and becomesThe failure of the
state , Then you need to restore the operations in the modified transaction to the state before the transaction is executed . let me put it another way , To undo the impact of failed transactions on the current database . We call this revocation processRoll back
. WhenRoll back
When the operation is completed , That is, the database is restored to the state before the transaction is executed , Let's say the officeSuspended
state . - The submitted (committed):
When one is inPartially submitted
The transaction in status will all the modified dataSync to disk
After that , We can say that the officeThe submitted
state .
A basic state transition diagram is shown below :
Two 、
1、 The completion process of the transaction :
- Open transaction
- A series of DML operation
- The state of the end of the transaction : The status of the submission (commit)、 Suspended status (rollback)
2、 According to the transaction
Use keywords start transaction or begin
start transaction It can be followed by :read only/ read write( Default )/ with consistent snapshot
Save it (savepoint): You can set a savepoint somewhere in the transaction , When an error is found, you can rollback to the savepoint .
3、 Implicit transaction
1、 keyword :autocommit ( The default is on)
2、 Turn off auto submit
The way ①:set autocommit = false;
The way ②:
边栏推荐
- The way of defi wealth password: Web3 entrepreneurship data
- Li Hongyi machine learning 2022.07.17 -- Introduction to deep learning and back propagation
- OSPF comprehensive experiment
- OSPF comprehensive experiment
- Deep understanding of C pointer 6.4.4 support tree with pointer
- Lazada obtains lazada product details API return value description
- 一份假Offer如何盗走了「Axie infinity」5.4亿美元?
- Cadence learning path: recommend an excellent packaging resource website
- [wechat applet] text (91/100)
- 【数字识别】基于Hopfield神经网络识别0-9数字附Matlab代码
猜你喜欢
Wechat applet -- "composition structure file function host environment"
李宏毅机器学习2022.07.17--深度学习简介和反向传播
消失之物 O(v) 删除某件物品
Analyzing the internal mechanism and breakthrough path of "Web3 paradox"
【C 項目】「通訊錄」
笔试强训第17天
LeetCode高频题:客栈选择问题,总共有多少种选择住宿的方案,保证晚上可以找到一家最低消费不超过 p 元的咖啡店小聚
CV-Model【4】:ResNet-34
安装Dart环境以及VsCode中Dart相关插件
基于matlab的最小支配集CDS仿真
随机推荐
Unity—动画
浅谈单元测试:测试和自动化中的利用
The next stop of NFT trading market is community led
加载properties文件
李宏毅机器学习2022.07.17--深度学习简介和反向传播
Iptables prevent nmap scanning
uni-app 官网教程
80、【backtrader基金策略】实现上证50ETF和创业板ETF轮动交易策略(2022-07-17更新)
Load properties file
After Clickhouse creates a table, the query table does not exist, and the results of each query of the same query statement are inconsistent
解析「Web3悖论」的内在机理与突破路径
运用滤波反投影的方法对图像进行重建matlab仿真
Install dart environment and dart related plug-ins in vscode
[csp-j2019] souvenirs (full backpack, stock trading)
旅行商的背包(二进制优化多重+0/1背包枚举体积))
Li Hongyi machine learning 2022.07.17 -- Introduction to deep learning and back propagation
How many of the 14 market bottom signals have been confirmed?
Vanishing object o (V) delete an item
How did a fake offer steal $540million from "axie infinity"?
Realization of chain team