当前位置:网站首页>mysql 事务隔离级别
mysql 事务隔离级别
2022-07-26 01:53:00 【i学长的猫】
| 事务隔离级别 | 脏读 | 不可重复读 | 幻读 |
| 读未提交(read-uncommitted) | 是 | 是 | 是 |
| 不可重复读(read-committed) | 否 | 是 | 是 |
| 可重复读(repeatable-read) | 否 | 否 | 是 |
| 串行化(serializable) | 否 | 否 | 否 |
1、读未提交:
(1)打开一个客户端A,并设置当前事务模式为read uncommitted(未提交读),查询表account的初始值:

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

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

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

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

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

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

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

(4)客户端B的事务提交

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

3、可重复读
(1)打开一个客户端A,并设置当前事务模式为repeatable read,查询表account的所有记录

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

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

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

(5)重新打开客户端B,插入一条新数据后提交

(6)在客户端A查询表account的所有记录,没有 查出 新增数据,所以没有出现幻读

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

mysql> set session transaction isolation level serializable; Query OK, 0 rows affected (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from account; +------+--------+---------+ | id | name | balance | +------+--------+---------+ | 1 | lilei | 10000 | | 2 | hanmei | 10000 | | 3 | lucy | 10000 | | 4 | lily | 10000 | +------+--------+---------+ 4 rows in set (0.00 sec)

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

mysql> set session transaction isolation level serializable; Query OK, 0 rows affected (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> insert into account values(5,'tom',0); ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

边栏推荐
- Software group verification
- Leetcode/ numbers that appear only once
- Image batch processing Gaussian filter noise reduction + peak signal-to-noise ratio calculation
- [independent station construction] Shopify seller: learn these points and double the sales volume of online stores!
- Speech comprehension - structural analysis exercise of fragment reading
- 【深入浅出玩转FPGA学习11----Testbench书写技巧1】
- E. OpenStreetMap (2D monotone queue)
- MPLS知识点
- 元素和小于等于阈值的正方形的最大边长(来源:力扣(LeetCode))
- [Verilog digital system design (Xia Yuwen) 4 ----- basic concepts of Verilog syntax 2]
猜你喜欢

BGP knowledge points summary

3、 Pinda general permission system__ pd-tools-swagger2

What is cross site scripting (XSS)?

Zhinai buys melons (DP backpack)

The sales volume has won the championship repeatedly. Is the secret of Wuling's success only low price?

Create a future and enjoy extraordinary | gbase Nantah General Motors unveiled opengauss Developer Day 2022

DialogRPT-Dialog Ranking Pretrained Transformers

There is no setter method in grpc list under flutter. How to use related attributes
![[Verilog digital system design (Xia Yuwen) 4 ----- basic concepts of Verilog syntax 2]](/img/fe/746ecaf4123072cca59d7510e9796c.png)
[Verilog digital system design (Xia Yuwen) 4 ----- basic concepts of Verilog syntax 2]

Navica工具把远程MySQL导入到本地MySQL数据库
随机推荐
Y77. Chapter IV Prometheus' monitoring system and practice -- Prometheus' service discovery mechanism (VIII)
达梦数据库表导入导出按钮灰色,导入不了dmp文件
Speech comprehension center comprehension summary
When everything can be metauniverse, the development of metauniverse seems to have entered a new stage of development
阿里云Redis开发规范
“蔚来杯“2022牛客暑期多校训练营2 G.[Link with Monotonic Subsequence] 分块构造
在Anaconda 中安装和使用R
Pt onnx ncnn conversion problem record (followed by yolov5 training)
Proto conversion dart | project uses protobuf | fluent uses grpc
登堂入室soc之arm汇编基础
DialogRPT-Dialog Ranking Pretrained Transformers
Go operation excel library excel use
pt-onnx-ncnn转换的问题记录(接yolov5训练)
flink sql 如何配置打印insert实参日志呢
Big view +500 cases, software teams should improve R & D efficiency in this way
2022 love analysis ― bank digitalization practice report
C language enumeration types and unions
Creation and management of MySQL database and table
Worthington产气荚膜梭菌神经氨酸酶的特征及测定
保护系统日志服务器和设备