当前位置:网站首页>Yyds dry inventory MySQL learning - how transactions are isolated
Yyds dry inventory MySQL learning - how transactions are isolated
2022-06-12 03:28:00 【Tangyuan learning java】
Preface
mysql The transactions in are used to ensure that when we operate the database , The whole operation process is either all successful , All or nothing , There will be no first half success , The second half fails ;
For example, the common transfer operations in our daily life , If there is no transaction support , It may appear that all the money in your card has been deducted , But Xiao Wang didn't receive the information ;
The transaction function is implemented at the engine level of the database ,InnoDB Support transactions ,MyISAM I won't support it ;
Let's introduce the concepts related to transactions
Catalog
- How to start a transaction
- The isolation level of the transaction
- Implementation of transaction isolation
Text
1. How to start a transaction
The startup mode is divided into explicit startup and implicit startup ;
- Explicit start
adopt begin perhaps start transaction Start transaction ;
adopt commit Commit transaction ;
adopt rollback Roll back the transaction ;
The benefits of explicit startup are startup and commit ( Roll back ) It always comes together , If you forget to submit, it will be easy to find out ;
This reduces the occurrence of long transactions ;
- Implicitly start
No need to pass begin perhaps start transaction Start transaction ;
When you execute a SQL When the sentence is ( such as select * from t), A transaction is started ;
Now if you set autocommit = 0, Then the transaction will not be automatically committed , It needs to be manually commit Submit perhaps rollback Roll back ; This may lead to long transactions ( Because there is no matching Start transaction command , So it's easy to forget Submission of orders );
And if the set autocommit = 1, The transaction is automatically committed , There is no need to manually commit; Each statement will automatically start a transaction , The transaction will be automatically committed after the statement is successfully executed , The transaction will be rolled back automatically after the statement fails to execute ;
But set up autocommit=1 There is a problem : It is the transaction that has lost its function , Data consistency cannot be guaranteed , For example, transfer operation ;
Recommended Practice : It is recommended to use explicit startup to Operational transaction ; meanwhile set autocommit = 1, This can prevent long transaction problems caused by implicit startup ;
2. The isolation level of the transaction
Yes 4 Kind of : Read uncommitted 、 Read the submission 、 Repeatable 、 Serialization ;
- Read uncommitted (READ-UNCOMMITED)
Definition : During the current transaction , You can read the uncommitted data of other transactions
For example, I now start a transaction A, Read out id=1 This data , At first, I read the result as 1;
At this time, I will start another transaction B, To update id=1 This line of data ;
here , I'll go back to work A Read data from , Will read the latest data ;
Let's use an example to demonstrate :
First, modify the isolation level to Read uncommitted :
View the current isolation level : You can see that the setting is successful
Start transactions explicitly A, Check at the same time id=1 The data of :
Start transactions explicitly B, Simultaneous updating id=1 The data of :
Back to business again A, Inquire about id=1 The data of :
You can see , Although we didn't commit the transaction B, But in business A You can still see the latest data in , This is the credit for reading uncommitted ;
The characteristic of this isolation level is that the latest data can always be read ; The disadvantages are obvious , In a transaction operation , Will read a different version of the data , This will give people an illusion of data disorder ;
The following examples of isolation levels are similar , The parameters of the isolation level are changed first , Then read and write in multiple transactions
- Read the submission
Definition : During the current transaction , You can read data submitted by other transactions , You can't read without submitting
- Repeatable ( Default )
Definition : During the current transaction , The data read is always consistent , That is, what data is read when the transaction is started , This data is read throughout the transaction , It won't change again
- Serialization
Definition : Understand literally , It is similar to single thread running , For example, there is a transaction writing a row of data , Then another transaction must wait for the previous transaction to commit before continuing ;
3. Implementation of transaction isolation
Transaction isolation is mainly realized through the concept of view ;
Suppose that the isolation level of the current transaction is repeatable :
So when the transaction starts , Will create a view , Used to record current data ;
At this point, no matter how other transactions update the data , None of the previously initiated transactions will be affected ;
Then we can say Through different views , Realize transaction isolation ;
Above, we use the read uncommitted isolation level as an example , Because the read uncommitted isolation level is the lowest , So I can't see the effect of isolation ;
Those interested can use the default isolation level ( Repeatable ) Repeat the above example , You will see that no matter how it is changed , Any previously initiated transaction will not be affected by the new transaction ;
summary
The transaction can be started explicitly , Implicitly start ; recommend Explicit start , adopt begin start-up ,commit Submit ,rooback Roll back ; meanwhile set autocommit = 1, This can prevent long transaction problems caused by implicit startup ;
There are four levels of transaction isolation : Read uncommitted 、 Read the submission 、 Repeatable 、 Serialization ; The isolation levels are incremented ;
Transaction isolation is achieved through Create different views To achieve : Transactions started at different times , Will create different views ;
边栏推荐
- Eight fallacies of distributed computing
- [Business Research Report] 2021 global mobile game player white paper - download link attached
- 顺序表与链表-----进阶
- oracle之序列
- go 递归无限极分类
- [Hongmeng] use the timer to play a simple game of robbing red envelopes
- postgresql基本介绍以及部署使用
- 1187_ C language implementation of hysteresis processing
- xml
- oracle之用户和表空间
猜你喜欢

Cloud native overview

central limit theorem

Community Conference | the mosn community will release version 1.0 and promote the evolution of the next generation architecture

Demand and business model innovation - demand 10- observation and document review

(idea)the file size(2.85M) exceeds configured limit(2.56M). Code insight features are not available

Three ways for ORALCE to process column to row conversion and finally generate table style data

Wechat applet project example - I have a paintbrush (painting)

绝对定位使盒子居中显示的三种方式

【点云压缩】Sparse Tensor-based Point Cloud Attribute Compression

技术经济与企业管理 复习 第四章
随机推荐
Sed command
PostgreSQL basic introduction and deployment
How do I make the mouse wheel work in the VB6 ide- How can I make mousewheel work in VB6 IDE?
Fastjson enables safemode, closes autotype, and removes security vulnerabilities
2020-12-12
2020-12-07
(idea)the file size(2.85M) exceeds configured limit(2.56M).Code insight features are not available问题
What is a request response pair called? [repeat] - what is a request response pair called? [duplicate]
2020-12-06
[Yugong series] March 2022 asp Net core Middleware - cross domain
Leetcode 6[finding rules] Z-transform the leetcode path of heroding
1187_ C language implementation of hysteresis processing
Redis gets the set of keys prefixed with XXX
laravel 8 选用 jwt 进行接口验证
1186_ Accumulation of embedded hardware knowledge_ Triode and three electrodes
The road of global evolution of vivo global mall -- multilingual solution
3769 moving stones (simulated)
The road of global evolution of vivo global mall -- multilingual solution
Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
[C language] dynamic memory allocation