What is a MySQL transaction? ?

MySQL transaction

Transaction is a logical unit of program execution composed of a series of operations to access and update data in the system.

  • Transaction syntax

  • Transaction characteristics

  • Transaction concurrency

  • Transaction isolation level

  • The lock situation of different isolation levels

  • Implicit commit

     

 

1, the syntax of the transaction

  1. start transaction;/ begin;

  2. commit; Make the current modification confirmation

  3. rollback; causes the current modification to be abandoned

⼆, ACID characteristics of transactions

  1. Originality (Atomicity)

    The originality of the transaction means that the transaction must be an original operation sequence unit. Each operation included in the transaction is only allowed to appear in one of two states during one execution

    • All executed successfully

    • All execution failed

    After the transaction starts, all operations are either done or not done at all. It is impossible to stop in the middle link. If an error occurs during the execution of the transaction, it will roll back to the state before the start of the transaction, and all operations will appear to have not occurred. In other words, business is an indivisible whole, just like the element learned in chemistry, it is the basic unit of material composition.

     

  2. Consistency

    The consistency of a transaction means that the execution of a transaction cannot destroy the integrity and consistency of database data. Before and after a transaction is executed, the database must be in a consistent state.

    For example, if you transfer money from account A to account B, it is impossible to deduct money from account A, but account B does not add money.

  3. Isolation

    Transaction isolation means that in a concurrent environment, concurrent transactions are isolated from each other. In other words, when different transactions operate on the same data concurrently, each transaction has its own complete data space.

    The internal operations of a transaction and the data used are isolated from other concurrent transactions, and each transaction executed concurrently cannot interfere with each other.

     

  4. Persistence (Duration)

    The durability of a transaction means that the data in the database must be permanently saved after the transaction is committed. Even if the server system crashes or the server is down and other failures. As long as the database is restarted, it must be able to restore it to the state after the transaction ends successfully.

Three, transaction concurrency issues

  • Dirty read: read data that has not been committed, transaction A reads the updated data of transaction B, and then B rolls back the operation, then the data read by A is dirty data.

  • Non-repeatable read: the same command returns different result sets (updates). Transaction A reads the same data multiple times, and transaction B updates and commits the data during the multiple reads of transaction A, causing the transaction A When reading the same data multiple times, the results are not consistent.

  • Phantom reading: In the process of repeated query, the amount of data changes (insert, delete).

Table Users Create ( 
    ID int Primary Key AUTO_INCREMENT, 
    name VARCHAR (10), 
    Age int, 
    Account int 
) = InnoDB Engine default charset = utf8mb4; 
INSERT INTO Users values (null, 'John Doe', 25,10000), (null , 'John Doe', 20,100), (null, ' Wang Wu', 23,0); 
basic Services operation 
1. Start transaction; / the begin; 
2. the commit; confirmation that current modification 
3. rollback; such that the current the modification was abandoned rollback

Fourth, transaction isolation level

Transaction isolation level Dirty read Non-repeatable Phantom reading
Read uncommitted (READ_UNCOMMITTED) allow allow allow
Read has been submitted (READ_COMMITTED) Prohibit allow allow
Repeatable read (REPEATABLE_READ) Prohibit Prohibit maybe
Sequential Reading (SERIALIZABLE) Prohibit Prohibit Prohibit

The four transaction isolation levels are from top to bottom. The higher the level, the worse the concurrency, and the higher the security. The default level of general data is read to submit or repeatable read.

 

View the isolation level of transactions in the current session

select @@tx_isolation;
+-----------------+
| @@tx_isolation  |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)

Set the transaction isolation level in the current session

mysql> set session transaction isolation level read uncommitted;
Query OK, 0 rows affected (0.00sec)
​
-- 读未提交
set session transaction isolation level read uncommitted;
​
-- 读已提交 READ_COMMITTED
set session transaction isolation level read committed;
​
​
-- 可重复读 REPEATABLE_READ
set session transaction isolation level repeatable read;
​
​
-- 顺序读 SERIALIZABLE
set session transaction isolation level SERIALIZABLE;

1. Read uncommitted (READ_UNCOMMITTED)

Read uncommitted, this isolation level allows dirty reads, and its isolation level is the lowest. In other words, if a transaction is processing certain data and updates it, but the transaction has not been completed at the same time, so the transaction has not been committed; at the same time, another transaction is allowed to access the data.

Dirty read example :

The following scenarios may occur when transaction A and transaction B are executed at the same time:

time Transaction A (Deposit) Transaction B (Withdrawal)
T1 Start transaction ——
T2 —— Start transaction
T3 —— Check the balance (1000 yuan)
T4 —— Withdraw 1,000 yuan (the balance is 0 yuan)
T5 Check the balance (0 yuan) ——
T6 —— Cancel the transaction (restore the balance 1,000 yuan)
T7 Deposit 500 yuan (the balance is 500 yuan) ——
T8 Commit transaction ——

The balance should be 1500 yuan. Please look at the T5 time point. Transaction A's balance queried at this time is 0. This data is dirty data. It is caused by Transaction B. Obviously, the transaction is not isolated.

2. Read submitted (READ_COMMITTED)

Reading committed is when a different transaction is executed, only the committed data can be obtained. In this way, there will be no dirty reads above. But performing the same read in the same transaction, the result is not consistent

Non-repeatable reading example

time Transaction A (Deposit) Transaction B (Withdrawal)
T1 Start transaction ——
T2 —— Start transaction
T3 —— Check the balance (1000 yuan)
T4 Check the balance (1000 yuan) ——
T5 —— Withdraw 1,000 yuan (the balance is 0 yuan)
T6 —— Commit transaction
T7 Check the balance (0 yuan) ——
T8 Commit transaction ——

In fact, transaction A did nothing except query twice. As a result, the money changed from 1000 to 0. This is the problem of non-repeatable reading.

3. Repeatable read (REPEATABLE_READ)

Repeatable read is to ensure that when the same data is read multiple times during the transaction processing, the value of the data is consistent with the start time of the transaction. Therefore, the transaction level limits non-repeatable reads and dirty reads, but phantom read data may occur.

Phantom reading

A phantom read refers to the same transaction operation. The read of the same data item is performed in the two time periods before and after, and inconsistent results may occur.

Weird update event

time Transaction A (Deposit) Transaction B (Withdrawal)
T1 Start transaction ——
T2 Query current all Start transaction
T3 —— Insert a piece of data
T4 Query all current data Commit transaction
T5 Make scope changes ——
T6 Query all current data ——
T7 Commit transaction ——

4. Sequential reading (SERIALIZABLE)

Sequential read is the strictest transaction isolation level. It requires all transactions to be queued and executed sequentially, that is, transactions can only be processed one by one and cannot be concurrent.

Five, the lock situation of different isolation levels

  1. Read uncommitted (RU): There are high-level locks and no gap locks. The difference between it and RC is that it can query unsubmitted data.

  2. Read committed (RC): There are high-level locks, no gap locks, and uncommitted data cannot be read.

  3. 可重复读(RR):有⾏级的锁,也有间隙锁,每次读取的数据都是⼀样的,并且没有幻读的情况。

  4. 序列化(S):有⾏级锁,也有间隙锁,读表的时候,就已经上锁了

六,隐式提交

DQL:查询语句

DML:写操作(添加,删除,修改)

DDL:定义语句(建库,建表,修改表,索引操作,存储过程,视图)

DCL:控制语⾔(给⽤户授权,或删除授权)

 

DDL(Data Define Language):都是隐式提交。

隐式提交:执⾏这种语句相当于执⾏commit; DDL

参考:https://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html

Guess you like

Origin blog.csdn.net/weixin_43515837/article/details/112004537