MySQL的四种事务隔离级别

一、理论原理

1.1 事务的基本要素(ACID)

1、原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做,不可能停滞在中间环节。事务执行过程中出错,会回滚到事务开始前的状态,所有的操作就像没有发生一样。也就是说事务是一个不可分割的整体,就像化学中学过的原子,是物质构成的基本单位。

2、一致性(Consistency):事务开始前和结束后,数据库的完整性约束没有被破坏 。比如A向B转账,不可能A扣了钱,B却没收到。

3、隔离性(Isolation):同一时间,只允许一个事务请求同一数据,不同的事务之间彼此没有任何干扰。比如A正在从一张银行卡中取钱,在A取钱的过程结束前,B不能向这张卡转账。

4、持久性(Durability):事务完成后,事务对数据库的所有更新将被保存到数据库,不能回滚。

1.2 事务的并发问题

1、脏读:事务A读取了事务B更新的数据,然后B回滚操作,那么A读取到的数据是脏数据

2、不可重复读:事务 A 多次读取同一数据,事务 B 在事务A多次读取的过程中,对数据作了更新并提交,导致事务A多次读取同一数据时,结果 不一致。

3、幻读:系统管理员A将数据库中所有学生的成绩从具体分数改为ABCDE等级,但是系统管理员B就在这个时候插入了一条具体分数的记录,当系统管理员A改结束后发现还有一条记录没有改过来,就好像发生了幻觉一样,这就叫幻读。

小结:不可重复读的和幻读很容易混淆,不可重复读侧重于修改,幻读侧重于新增或删除。解决不可重复读的问题只需锁住满足条件的行,解决幻读需要锁表

1.3 MySQL事务隔离级别

事务隔离级别脏读不可重复读幻读
未提交读(read-uncommitted)
已提交读(read-committed)
可重复读(repeatable-read)
序列化(serializable)

mysql 默认的事务隔离级别为 repeatable-read:

mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation  |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set

二、举例说明各个隔离级别的情况

2.1、未提交读(read-uncommitted)

(1)打开一个客户端 【A】,并设置当前事务模式为read uncommitted(未提交读),查询表 tb_user 的初始值:

mysql> set session transaction isolation level read uncommitted;  --A客户端设置当前事务模式
Query OK, 0 rows affected

mysql> start transaction;	--开启一个事务
Query OK, 0 rows affected

mysql> select * from tb_user;	--执行一个sql操作,此时并未提交
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 500     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

(2)在客户端A的事务提交之前,打开另一个客户端 【B】,更新表tb_user :

mysql> set session transaction isolation level read uncommitted;   --B客户端设置当前事务模式
Query OK, 0 rows affected 

mysql> select @@tx_isolation;
+------------------+
| @@tx_isolation   |
+------------------+
| READ-UNCOMMITTED |
+------------------+
1 row in set

mysql> start transaction;		--开启一个事务
Query OK, 0 rows affected

mysql> update tb_user set balance=balance-50 where id = 1;	--修改id为1的值,但是还没有提交事务
Query OK, 1 row affected
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from tb_user;		--查询,可以看出,数据库数据变更
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 450     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

(3)这时,虽然客户端B的事务还没提交,但是客户端 【A】 就可以查询到B已经更新的数据:

mysql> select * from tb_user;		-- 这是此时在A客户端执行查询出来的结果,可以看到,结果已经变更
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 450     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

(4)一旦客户端 【B】 的事务因为某种原因回滚,所有的操作都将会被撤销,那客户端A查询到的数据其实就是脏数据:

mysql> rollback;	--B客户端因为个中原因,回滚了事务,
Query OK, 0 rows affected

mysql> select * from tb_user;	--此时查询的数据的确回滚为修改之前的数据了
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 500     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

(5)在客户端 【A】 执行更新语句update account set balance = balance - 50 where id =1,张三的balance没有变成400,居然是450,是不是很奇怪,数据不一致啊。在应用程序中,我们会用450-50=400,并不知道其他会话回滚了,要想解决这个问题可以采用读已提交的隔离级别

mysql> update tb_user set balance=balance-50 where id = 1;  	--	A客户端依然可以是用回滚后的数据做操作,而我们之前查询的数据是450
Query OK, 1 row affected
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from tb_user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 450     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

2.2 已提交读(read committed)

(1)打开一个客户端 【A】,并设置当前事务模式为read committed(读已提交),查询表 tb_user 的所有记录:

mysql> set session transaction isolation level read committed;  --A客户端设置为读已提交
Query OK, 0 rows affected

mysql> start transaction;
Query OK, 0 rows affected

mysql> select * from tb_user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 450     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

(2)在客户端A的事务提交之前,打开另一个客户端 【B】,更新表 tb_user :

mysql> select * from tb_user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 450     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

mysql> set session transaction isolation level read committed;  -- B设置为 read committed
Query OK, 0 rows affected

mysql> start transaction;		--同时开启一个事务
Query OK, 0 rows affected

mysql> update tb_user set balance=balance-50 where id =1;
Query OK, 1 row affected
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from tb_user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 400     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

(3)这时,客户端B的事务还没提交,客户端 【A】 不能查询到B已经更新的数据,解决了脏读问题:

mysql> select * from tb_user; 		--A客户端查询依然是之前的450
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 450     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

(4)此时,客户端 【B】 的事务提交。


mysql> commit;		--B客户端提交事务
Query OK, 0 rows affected

(5)客户端A执行与上一步相同的查询,结果 与上一步不一致,即产生了不可重复读的问题。

mysql> select * from tb_user;		--B事务提交之前,A客户端的查询结果
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 450     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

mysql> select * from tb_user;		--B事务提交之后,A客户端的查询结果
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 400     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

2.3 可重复读(repeatable read)

(1)打开一个客户端 【A】,并设置当前事务模式为repeatable read,查询表 tb_user 的所有记录。

mysql> set session transaction isolation level repeatable read;	
Query OK, 0 rows affected

mysql> start transaction;
Query OK, 0 rows affected

mysql> select * from tb_user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 450     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
5 rows in set

(2)在客户端A的事务提交之前,打开另一个客户端 【B】,更新表tb_user 并提交。


mysql> set session transaction isolation level repeatable read;	
Query OK, 0 rows affected

mysql> start transaction;
Query OK, 0 rows affected

mysql> update tb_user set balance=balance-50 where id =1;
Query OK, 1 row affected
Rows matched: 1  Changed: 1  Warnings: 0
mysql> commit;
Query OK, 0 rows affected

mysql> select * from tb_user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 400     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
|  4 | lisa | 666     |
|  5 | lisa | 666     |
+----+------+---------+
5 rows in set

mysql> 

(3)在客户端 【A】 查询表tb_user 的所有记录,与步骤(1)查询结果一致,没有出现不可重复读的问题。

mysql> select * from tb_user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 450     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
|  4 | lisa | 666     |
|  5 | lisa | 666     |
+----+------+---------+
5 rows in set

(4)在客户端A,接着执行 update tb_user set balance=balance-50 where id =1,balance没有变成450-50=400,而是400-50=350,数据的一致性倒是没有被破坏。可重复读的隔离级别下使用了MVCC机制,select操作不会更新版本号,是快照读(历史版本);insert、update和delete会更新版本号,是当前读(当前版本)。

mysql> select * from tb_user;   --B提交事务后,A的查询操作
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 450     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
|  4 | lisa | 666     |
|  5 | lisa | 666     |
+----+------+---------+
5 rows in set

mysql> update tb_user set balance=balance-50 where id =1;	-- A更新后,结果保证了数据的最终最终一致性
Query OK, 1 row affected
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from tb_user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 350     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
|  4 | lisa | 666     |
|  5 | lisa | 666     |
+----+------+---------+
5 rows in set

2.4 序列化

(1)打开一个客户端A,并设置当前事务模式为serializable,查询表tb_user 的初始值:

mysql> set session transaction isolation level serializable;
Query OK, 0 rows affected

mysql> start transaction;
Query OK, 0 rows affected

mysql> select * from tb_user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 250     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

(2)打开一个客户端B,并设置当前事务模式为serializable,插入一条记录报错,表被锁了插入失败,mysql中事务隔离级别为serializable时会锁表,因此不会出现幻读的情况,这种隔离级别并发性极低,开发中很少会用到。

mysql> set session transaction isolation level serializable;
Query OK, 0 rows affected

mysql> start transaction;
Query OK, 0 rows affected

mysql> select * from tb_user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | 张三 | 250     |
|  2 | 李四 | 8000    |
|  3 | 王五 | 3000    |
+----+------+---------+
3 rows in set

mysql> update tb_user set balance=balance-50 where id =1; --更新,插入操作都不可以执行
1205 - Lock wait timeout exceeded; try restarting transaction

三、补充

1、事务隔离级别为读提交时,写数据只会锁住相应的行

2、事务隔离级别为可重复读时,如果检索条件有索引(包括主键索引)的时候,默认加锁方式是next-key 锁;如果检索条件没有索引,更新数据时会锁住整张表。一个间隙被事务加了锁,其他事务是不能在这个间隙插入记录的,这样可以防止幻读。

3、事务隔离级别为串行化时,读写数据都会锁住整张表

4、隔离级别越高,越能保证数据的完整性和一致性,但是对并发性能的影响也越大。

5、MYSQL MVCC实现机制参考链接:https://blog.csdn.net/whoamiyang/article/details/51901888

6、关于next-key 锁可以参考链接:https://blog.csdn.net/bigtree_3721/article/details/73731377

实践的时候,有各种懵逼各种坑,还要细细品味才行。

原文链接 https://www.cnblogs.com/huanongying/p/7021555.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值