当前位置:网站首页>MySQL transaction isolation level
MySQL transaction isolation level
2022-07-26 01:59:00 【I senior cat】
| Transaction isolation level | Dirty reading | It can't be read repeatedly | Fantasy reading |
| Read uncommitted (read-uncommitted) | yes | yes | yes |
| It can't be read repeatedly (read-committed) | no | yes | yes |
| Repeatable (repeatable-read) | no | no | yes |
| Serialization (serializable) | no | no | no |
1、 Read uncommitted :
(1) Open a client A, And set the current transaction mode to read uncommitted( Uncommitted read ), Query table account The initial value of the :

(2) On the client side A Before the transaction is committed , Open another client B, Update table account:

(3) At this time , Although the client B The business of has not been submitted , But the client A You can find B Updated data :

(4) Once the client B The transaction of is rolled back for some reason , All operations will be undone , That client A The data queried is actually dirty data :

(5) On the client side A Execute UPDATE statement update account set balance = balance - 50 where id =1,lilei Of balance Did not become 350, was 400, Isn't that weird? , The data are inconsistent , It would be naive of you to think so 了 , In the application , We will use 400-50=350, I didn't know that other sessions rolled back , To solve this problem, read committed isolation level

2、 Read submitted
(1) Open a client A, And set the current transaction mode to read committed( Uncommitted read ), Query table account All records :

(2) On the client side A Before the transaction is committed , Open another client B, Update table account:

(3) At this time , client B The business of has not been submitted , client A Can't find B Updated data , Solved the problem of dirty reading :

(4) client B Transaction commit for

(5) client A Execute the same query as in the previous step , result Inconsistent with the previous step , That is to say, there is the problem of non repetition

3、 Repeatable
(1) Open a client A, And set the current transaction mode to repeatable read, Query table account All records

(2) On the client side A Before the transaction is committed , Open another client B, Update table account And submit

(3) On the client side A Query table account All records , And steps (1) The results are consistent , There is no non repeatable reading problem

(4) On the client side A, Then perform update balance = balance - 50 where id = 1,balance Did not become 400-50=350,lilei Of balance It's worth the steps (2) Medium 350 To calculate , So it is 300, The consistency of the data has not been broken . Used under the isolation level of repeatable reading MVCC Mechanism ,select Operation will not update version number , It's snapshot reading ( Version history );insert、update and delete Will update version number , It's the current reading ( current version ).

(5) Reopen the client B, Insert a new data and submit

(6) On the client side A Query table account All records , No, Find out The new data , So there is no unreal reading

4. Serialization
(1) Open a client A, And set the current transaction mode to serializable, Query table account The initial value of the :

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) Open a client B, And set the current transaction mode to serializable, Insert a record to report an error , Table locked insert failed ,mysql The transaction isolation level in is serializable It locks the watch , So there will be no unreal reading , This isolation level is extremely low in concurrency , It's rarely used in development .

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

边栏推荐
- npm ERR! code ETIMEDOUTnpm ERR! syscall connectnpm ERR! errno ETIMEDOUTnpm ERR! network request t
- Leetcode/ numbers that appear only once
- proto转换Dart | 项目使用Protobuf | flutter 使用grpc
- Dest0g3 520 orientation (under update)
- Republishing foundation and configuration
- Make and makefile summary I
- 劳驾问一下各位老师 oracle 到pg cdc oracle 那边字段大写 pg 这边小写 同
- Remember a laravel problem script @php artist package:discover handling the post autoload dump event returned with
- flink sql 如何配置打印insert实参日志呢
- How does Flink SQL configure to print the insert parameter log
猜你喜欢

IP address of the network

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

Implementation of recommendation system collaborative filtering in spark
![4QAM, 16QAM modulation and demodulation simulation circuit, observe and analyze QAM constellation and bit error rate curve [matlab code]](/img/95/5b9a2347d20cc5da0d2920b7f583ce.png)
4QAM, 16QAM modulation and demodulation simulation circuit, observe and analyze QAM constellation and bit error rate curve [matlab code]

D. Rating compression (thinking + double pointer)

Composition API的优势

Protect syslog servers and devices

Characteristics and determination of neuraminidase from Clostridium perfringens in Worthington

餐饮连锁门店重塑增长背后的数字化转型

Three modes of CPU
随机推荐
Navica工具把远程MySQL导入到本地MySQL数据库
Summary after reading "poor dad and rich dad"
IP address of the network
FFT is used to estimate the image resampling factor after interpolation
The work of robot engineering and the puzzle of postgraduate entrance examination "volume" supplement
Why does the debugger display the wrong function
BGP knowledge points summary
# Dest0g3 520迎新赛(更新中)
npm ERR! code ETIMEDOUTnpm ERR! syscall connectnpm ERR! errno ETIMEDOUTnpm ERR! network request t
1205 Lock wait timeout exceeded; try restarting transaction处理
F. Equal multisets (greedy)
Dataframe modifies the value of a row or column position
Overview of database stress testing methods
阿里云Redis开发规范
软件加群验证
How to modify Oracle functions?
【Verilog数字系统设计(夏宇闻)3-----Verilog语法的基本概念1】
What is the difference between for... In... And for... Of
y77.第四章 Prometheus大厂监控体系及实战 -- prometheus的服务发现机制(八)
达梦数据库表导入导出按钮灰色,导入不了dmp文件