MySQL transaction review
1.1 Overview of the transaction
- Transaction (transaction), a transaction is a complete business logic unit, can not be divided,
For example: bank account transfer, transfer from account A to account B, you need to execute two update statements,
An update increases,
An update is reduced,
The above two update and sentences succeed and fail at the same time.
It is better to allow one success and one failure,
To ensure that the above two DML statements succeed and fail at the same time, then you need to use the database "transaction mechanism"
- The statements related to transactions are only DML language, database manipulation statements, (insert delete update)
Why? Because these statements are related to the "data" in the database,
The existence of transactions is to ensure the integrity and security of data
- If all businesses only need a DML language, do they still need a transaction mechanism?
No need for affairs;
But the actual situation is not like this, usually a transaction, "business", requires multiple DML languages to jointly complete
1.2 Principle of transaction
mysql five major statements
-
DQL database query statement
-
DML database manipulation statement
-
DDL database definition language
-
TCL (transaction: commit rollback) savepoint savepoint
-
DCL: (authorization)
![image 20200904084955651](https://s1.ax1x.com/2020/09/04/wFrUpQ.png)
1.3 The characteristics of the transaction
Four characteristics of transaction ACID
-
A. ( Atomicity ) Atomicity : A transaction is the smallest unit and cannot be divided
-
C ( consistency ) consistency: The transaction must ensure that multiple DML statements succeed or fail at the same time.
-
I ( isolation ) isolation: transaction A must have isolation from transaction B equivalent to thread
-
D ( durability ): Durability: Durability means that the final data must be persisted to the hard disk file before the transaction is considered successful.
1.4 transaction isolation
About isolation between transactions
There are isolation levels for transaction isolation, which theoretically include four:
In fact, they start in second gear:
- The first level: Read uncommitted (read uncommitted), the current transaction can read the other party's uncommitted transaction
Reading uncommitted data will have dirty reads (dirty reads are reading dirty data that was not committed by the previous transaction) phenomenon: it means that dirty data has been read
- The second level: **Read Committed** We can read the data submitted by the other party's transaction
The problem with read committed is: non-repeatable read (non-repeatable read is to re-read the submitted data of the previous transaction)
- The third level: Repeatable Reads (repeatable read)
This isolation level solves: non-repeatable read
There is a problem: the data read is a phantom
Phantom read (virtual read) refers to reading data inserted by another transaction in one transaction, which results in inconsistent reading before and after.
- The fourth level (Serializable): serialization, serialized reading solves all problems,
Disadvantages: low efficiency and need to queue up transactions
Oracle starts in second gear by default, read has been submitted
Mysql default isolation level, repeatable reading
1.5 Demo business
Mysql transaction is automatically submitted by default, (what is automatic submission, as long as any DML statement is executed, it will be submitted once)
First, set the isolation transaction level
1. read uncommitted (read uncommitted)
Read uncommitted
Open two windows, modify the data of one transaction, one transaction can be queried,
Set the isolation level of the transaction
set global transaction isolation level read uncommitted;
View msyql isolation level;
select @@global.tx_isolation;
![image 20200904085613662](https://s1.ax1x.com/2020/09/04/wFrtfg.png)
After setting the isolation level, leave.
![image 20200904085632680](https://s1.ax1x.com/2020/09/04/wFralj.png)
2. read committed (read committed)
Can read the data submitted by other transactions (the default isolation level of most databases)
3. repeatable read (repeatable read)
Mysql default isolation level
![image 20200904085811828](https://s1.ax1x.com/2020/09/04/wFrBmq.png)
When we set the isolation level of the current conversation to repeatable read, the current conversation can be read repeatedly, that is, the result set of each read is the same. In fact, a copy of the data is backed up, and the phenomenon of phantom reading occurs.
4. serializable
![image 20200904085843238](https://s1.ax1x.com/2020/09/04/wFrd6s.png)