当前位置:网站首页>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

边栏推荐
- E2. escape the maze (hard version)
- How to display numbers / English time in Excel
- Overview of database stress testing methods
- MySQL locking table problem
- Is it safe to buy funds in stock accounts? Professional answers
- leetcode/只出现一次的数字
- FFT is used to estimate the image resampling factor after interpolation
- Cross Site Request Forgery (CSRF): impact, examples, and Prevention
- npm ERR! code ETIMEDOUTnpm ERR! syscall connectnpm ERR! errno ETIMEDOUTnpm ERR! network request t
- D. Permutation restoration (greedy + double pointer)
猜你喜欢

图像批处理高斯滤波降噪+峰值信噪比计算

元素和小于等于阈值的正方形的最大边长(来源:力扣(LeetCode))

3、 Pinda general permission system__ pd-tools-swagger2
SQL injection tutorial: learn through examples

Digital transformation behind the reshaping growth of catering chain stores

Video game quiz? I think it's useless. It's better to do these well!

excel中怎么显示数字/英文时间

How to display numbers / English time in Excel

怎么使用宝塔面板把node全栈项目部署到服务器上

SVN版本控制分支、合并功能使用
随机推荐
IP address of the network
Leetcode/ numbers that appear only once
SQLyog数据导入导出图文教程
G. Count the trains (thought set + two points)
Worthington核酸酶、微球菌相关研究及测定方案
FFT用于估计插值后的图像重采样因子
4QAM, 16QAM modulation and demodulation simulation circuit, observe and analyze QAM constellation and bit error rate curve [matlab code]
SQL injection tutorial: learn through examples
leetcode/只出现一次的数字
My Mysql to MySQL data table synchronization, only the code written in the first order will take effect, and the rest will not take effect. This may be
Is it safe to buy funds in stock accounts? Professional answers
Worthington木瓜蛋白酶丨从纯化的蛋白聚糖生产糖肽(附文献)
P3166 number triangle (tolerance and exclusion +gcd)
Network layer 2 and layer 3 forwarding
Make and makefile summary I
How idea can quickly delete recently opened projects
[in simple terms, play with FPGA learning 11 --- testbench writing skills 1]
[in simple terms, play with FPGA learning 11 --- testbench writing skills 2]
Niuke - bm39 serialized binary tree [hard]
proto转换Dart | 项目使用Protobuf | flutter 使用grpc