Article Directory
1. Teaching video
Teaching explanation video: video address
2. Business Introduction
A transaction is a collection of operations. A transaction will submit or revoke an operation request to the system together with all operations as a whole同时成功
, that is , these operations are either or 同时失败
.
Three, transaction operation
1. View the transaction submission method
SELECT @@AUTOCOMMIT;
2. Set the transaction submission method
1为自动提交
, 0为手动提交
, this setting is only 会话
valid for the current one.
SET @@AUTOCOMMIT = 0;
3. Open the transaction
START TRANSACTION 或 BEGIN TRANSACTION;
4. Submit the transaction
COMMIT;
5. Roll back the transaction
ROLLBACK;
Sample code:
-- 设置为手动提交
set @@autocommit = 0;
-- 查询提交方式
select @@autocommit;
-- 开启事务
start transaction ;
-- 更新数据
update tests set num = num - 100 where id = 2;
-- 提交事务
COMMIT;
-- 回滚事务
ROLLBACK;
4. ACID, the four major characteristics of transactions
- Atomicity: A transaction is the smallest indivisible unit of operation, either all succeed or all fail.
- Consistency: When a transaction completes, all data must be in a consistent state.
- Isolation: The isolation mechanism provided by the database system ensures that transactions run in an independent environment that is not affected by external concurrent operations.
- Durability: Once a transaction is committed or rolled back, its changes to the data in the database are permanent.
Five, the problem of concurrent transactions
question | describe |
---|---|
dirty read | A transaction reads data that has not yet been committed by another transaction |
non-repeatable read | A transaction reads the same record successively, but the data read twice is different |
Phantom reading | When a transaction queries data according to conditions, there is no corresponding data row, but when inserting data, it is found that this row of data already exists |
6. Concurrent transaction isolation level
isolation level | dirty read | non-repeatable read | Phantom reading |
---|---|---|---|
Read uncommitted | √ | √ | √ |
Read committed | × | √ | √ |
Repeatable Read (default) | × | × | √ |
Serializable | × | × | × |
- √ indicates that the problem will occur under the current isolation level.
- Serializable performance
最低
; Read uncommitted performance最高
, data security最差
.
-- 查看事务隔离级别:
SELECT @@TRANSACTION_ISOLATION; -- 5.7.2之后
SELECT @@TX_ISOLATION; -- 5.7.2之前
-- 设置事务隔离级别:
SET [ SESSION | GLOBAL ] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE };
-- SESSION 是会话级别,表示只针对当前会话有效,GLOBAL 表示对所有会话有效
7. Code example
1. Dirty read example
Transaction A
SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
set @@autocommit = 0;
start transaction;
update tests set num = num - 100 where id = 2;
ROLLBACK;
Transaction B
SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
set @@autocommit = 0;
start transaction;
select * from tests where id = 2;
COMMIT;
We first set the isolation level of both transactions to 读未提交
, when transaction A executes the update command but has not yet executed the rollback , the data selected by transaction B 已经update后的数据
is dirty read.
2. Non-repeatable read instance
Transaction A
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
set @@autocommit = 0;
start transaction;
update tests set num = num - 100 where id = 2;
COMMIT;
Transaction B
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
set @@autocommit = 0;
start transaction;
select * from tests where id = 2;
COMMIT;
We will find that in A transaction 提交之前
and 提交之后
we have the same query statement in B transaction, the result of the query 不一致
, this is the problem of non-repeatable read.
3. Examples of phantom reading
Transaction A
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
set @@autocommit = 0;
start transaction;
insert into tests VALUE(3, 1000);
COMMIT;
Transaction B
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
set @@autocommit = 0;
start transaction;
select * from tests where id = 3;
insert into tests VALUE(3, 1000);
commit;
After transaction A executes insert but has not submitted yet , transaction B queries the data with id 3 at this time 查不到
, and then transaction A proceeds . 提交
At this time, transaction B still has 查不到
data with id 3, but insert will report 主键冲突
an error again.
4. Realization of serialization
Transaction A
use db_online_supermarket_system;
SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE ;
set @@autocommit = 0;
start transaction;
select * from tests where id = 10;
commit;
Transaction B
use db_online_supermarket_system;
SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE ;
set @@autocommit = 0;
start transaction;
insert into tests VALUE(10, 1000);
commit;
If we 在A事务先执行了select查询语句之后
do, when our B transaction is executed to insert 阻塞等待
, we will not continue to execute B transaction until A transaction is committed.