Mysql事务隔离级别之读提交详解

编程学习 2021-07-05 14:37www.dzhlxh.cn编程入门
这篇文章主要介绍了Mysql事务隔离级别之读提交详解,长沙网络推广觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧

查看mysql 事务隔离级别

mysql> show variables like '%isolation%';
+---------------+----------------+
| Variable_name | Value     |
+---------------+----------------+
| tx_isolation | READ-COMMITTED |
+---------------+----------------+
1 row in set (0.00 sec)

可以看到当前的事务隔离级别为 READ-COMMITTED 读提交

狼蚁网站SEO优化看看当前隔离级别下的事务隔离详情,开启两个查询终端A、B。

狼蚁网站SEO优化有一个order表,初始数据如下

mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   1 |
+----+--------+
1 row in set (0.00 sec)

第一步,在A,B中都开启事务

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

第二步查询两个终端中的number值

A

 mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   1 |
+----+--------+
1 row in set (0.00 sec)

B

 mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   1 |
+----+--------+
1 row in set (0.00 sec)

第三步将B中的number修改为2,但不提交事务

mysql> update `order` set number=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

第四步查询A中的值

mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   1 |
+----+--------+
1 row in set (0.00 sec)

发现A中的值并没有修改。

第五步,提交事务B,再次查询A中的值

B

mysql> commit;
Query OK, 0 rows affected (0.01 sec)

A

mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   2 |
+----+--------+
1 row in set (0.00 sec)

发现A中的值已经更改

第六步,提交A中的事务,再次查询A,B的值。

A

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   2 |
+----+--------+
1 row in set (0.00 sec)

B

mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   2 |
+----+--------+
1 row in set (0.00 sec)

发现A,B中的值都更改为2了。

狼蚁网站SEO优化给一个简单的示意图


我们可以看到,在事务隔离级别为读已提交 的情况下,当B中事务提交了之后,即使A未提交也可以读到B事务提交的结果。这样解决了脏读的问题。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板