当前位置:   article > 正文

MySQL事务隔离级别:可重复读、读已提交、读未提交。实操

读已提交

MySQL的事务隔离级别:

目录

一、可重复读(默认) REPEATABLE-READ;

二、读已提交  READ-COMMITTED; 


一、可重复读(默认) REPEATABLE-READ;

准备实操的表和数据:

  1. -- ----------------------------
  2. -- Table structure for account
  3. -- ----------------------------
  4. DROP TABLE IF EXISTS `account`;
  5. CREATE TABLE `account` (
  6. `id` int(11) NOT NULL,
  7. `name` varchar(255) DEFAULT NULL,
  8. `balance` decimal(10,2) DEFAULT NULL,
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  11. -- ----------------------------
  12. -- Records of account
  13. -- ----------------------------
  14. INSERT INTO `account` VALUES ('1', 'lilei', '100.00');
  15. INSERT INTO `account` VALUES ('2', 'hanmei', '1090.00');
  16. INSERT INTO `account` VALUES ('3', 'lucy', '2.00');
  17. INSERT INTO `account` VALUES ('4', 'wanggang', '99.00');

分别通过两个SQL客户端来实际操作测试可重复读;

1、开启事务A:

  1. -- 开启事务
  2. mysql> start transaction;
  3. Query OK, 0 rows affected
  4. -- 查询account表所有记录
  5. mysql> select * from account;
  6. +----+----------+---------+
  7. | id | name | balance |
  8. +----+----------+---------+
  9. | 1 | lilei | 100 |
  10. | 2 | hanmei | 1090 |
  11. | 3 | lucy | 2 |
  12. | 4 | wanggang | 99 |
  13. +----+----------+---------+
  14. 4 rows in set

2、开启事务B:

  1. -- 开启事务
  2. mysql> start transaction;
  3. Query OK, 0 rows affected
  4. -- 查询account表所有记录
  5. mysql> select * from account;
  6. +----+----------+---------+
  7. | id | name | balance |
  8. +----+----------+---------+
  9. | 1 | lilei | 100 |
  10. | 2 | hanmei | 1090 |
  11. | 3 | lucy | 2 |
  12. | 4 | wanggang | 99 |
  13. +----+----------+---------+
  14. 4 rows in set

3、事务B操作数据:

  1. -- 更新id=1 的balance 增加50
  2. mysql> update account set balance = balance + 50 where id = 1;
  3. Query OK, 1 row affected
  4. Rows matched: 1 Changed: 1 Warnings: 0
  5. -- 当前事务查询id=1 的balance 已经增加50
  6. mysql> select * from account;
  7. +----+----------+---------+
  8. | id | name | balance |
  9. +----+----------+---------+
  10. | 1 | lilei | 150 |
  11. | 2 | hanmei | 1090 |
  12. | 3 | lucy | 2 |
  13. | 4 | wanggang | 99 |
  14. +----+----------+---------+
  15. 4 rows in set

4、事务A查询验证【可重复读】:

  1. -- 查询account 数据还是第一步的数据,没有变化;支持可重复读
  2. mysql> select * from account;
  3. +----+----------+---------+
  4. | id | name | balance |
  5. +----+----------+---------+
  6. | 1 | lilei | 100 |
  7. | 2 | hanmei | 1090 |
  8. | 3 | lucy | 2 |
  9. | 4 | wanggang | 99 |
  10. +----+----------+---------+
  11. 4 rows in set

5、事务B提交事务,查询数据变化:

  1. -- 提交事务
  2. mysql> commit;
  3. Query OK, 0 rows affected
  4. -- 查询当前account数据
  5. mysql> select * from account;
  6. +----+----------+---------+
  7. | id | name | balance |
  8. +----+----------+---------+
  9. | 1 | lilei | 150 |
  10. | 2 | hanmei | 1090 |
  11. | 3 | lucy | 2 |
  12. | 4 | wanggang | 99 |
  13. +----+----------+---------+
  14. 4 rows in set

6、事务A查询验证数据【可重复读】:

  1. -- 查询account数据,没有变化;支持可重复读
  2. mysql> select * from account;
  3. +----+----------+---------+
  4. | id | name | balance |
  5. +----+----------+---------+
  6. | 1 | lilei | 100 |
  7. | 2 | hanmei | 1090 |
  8. | 3 | lucy | 2 |
  9. | 4 | wanggang | 99 |
  10. +----+----------+---------+
  11. 4 rows in set

7、事务A提交事务,查询到最新数据:

  1. -- 提交当前事务
  2. mysql> commit;
  3. Query OK, 0 rows affected
  4. -- 再次查询account;此时的数据为最新的数据。
  5. mysql> select * from account;
  6. +----+----------+---------+
  7. | id | name | balance |
  8. +----+----------+---------+
  9. | 1 | lilei | 150 |
  10. | 2 | hanmei | 1090 |
  11. | 3 | lucy | 2 |
  12. | 4 | wanggang | 99 |
  13. +----+----------+---------+
  14. 4 rows in set

MySQL的可重复读,是通过MVCC多版本控制机制来实现的。

二、读已提交  READ-COMMITTED; 

1、开启事务A ,查询account数据;

  1. mysql> select @@tx_isolation;
  2. +----------------+
  3. | @@tx_isolation |
  4. +----------------+
  5. | READ-COMMITTED |
  6. +----------------+
  7. 1 row in set
  8. mysql> start transaction;
  9. mysql> select * from account;
  10. +----+----------+---------+
  11. | id | name | balance |
  12. +----+----------+---------+
  13. | 1 | lilei | 150 |
  14. | 2 | hanmei | 1090 |
  15. | 3 | lucy | 2 |
  16. | 4 | wanggang | 99 |
  17. +----+----------+---------+
  18. 4 rows in set

2、开启事务B ,查询account数据;

  1. mysql> select @@tx_isolation;
  2. +----------------+
  3. | @@tx_isolation |
  4. +----------------+
  5. | READ-COMMITTED |
  6. +----------------+
  7. 1 row in set
  8. mysql> start transaction;
  9. mysql> select * from account;
  10. +----+----------+---------+
  11. | id | name | balance |
  12. +----+----------+---------+
  13. | 1 | lilei | 150 |
  14. | 2 | hanmei | 1090 |
  15. | 3 | lucy | 2 |
  16. | 4 | wanggang | 99 |
  17. +----+----------+---------+
  18. 4 rows in set

3、事务B ,更新account id = 1,balance+50 ;查询数据已经变化

  1. mysql> update account set balance = balance + 50 where id = 1;
  2. Query OK, 1 row affected
  3. Rows matched: 1 Changed: 1 Warnings: 0
  4. mysql> select * from account;
  5. +----+----------+---------+
  6. | id | name | balance |
  7. +----+----------+---------+
  8. | 1 | lilei | 200 |
  9. | 2 | hanmei | 1090 |
  10. | 3 | lucy | 2 |
  11. | 4 | wanggang | 99 |
  12. +----+----------+---------+
  13. 4 rows in set
  14. mysql>

4、事务A ,查询account数据,验证数据是否可重复读;

  1. -- 事务B修改操作,当前事务B未提交;没有影响事务A的数据。
  2. mysql> select * from account;
  3. +----+----------+---------+
  4. | id | name | balance |
  5. +----+----------+---------+
  6. | 1 | lilei | 150 |
  7. | 2 | hanmei | 1090 |
  8. | 3 | lucy | 2 |
  9. | 4 | wanggang | 99 |
  10. +----+----------+---------+
  11. 4 rows in set

5、事务B ,事务提交,查询account数据,已经修改完成;

  1. mysql> commit;
  2. Query OK, 0 rows affected
  3. mysql> select * from account;
  4. +----+----------+---------+
  5. | id | name | balance |
  6. +----+----------+---------+
  7. | 1 | lilei | 200 |
  8. | 2 | hanmei | 1090 |
  9. | 3 | lucy | 2 |
  10. | 4 | wanggang | 99 |
  11. +----+----------+---------+
  12. 4 rows in set

6、事务A ,查询account数据,验证数据是否可重复读;

  1. -- 事务A并没有结束,但是数据已经变化了。证明读已提交这个隔离级别,是不可重复读的。
  2. mysql> select * from account;
  3. +----+----------+---------+
  4. | id | name | balance |
  5. +----+----------+---------+
  6. | 1 | lilei | 200 |
  7. | 2 | hanmei | 1090 |
  8. | 3 | lucy | 2 |
  9. | 4 | wanggang | 99 |
  10. +----+----------+---------+
  11. 4 rows in set

后续增加读未提交、串行化实操案例。

-------------欢迎各位留言交流,如有不正确的地方,请予以指正。【Q:981233589】

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/239616?site
推荐阅读
相关标签
  

闽ICP备14008679号