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

边栏推荐
- Composition API的优势
- DialogRPT-Dialog Ranking Pretrained Transformers
- Worthington nuclease and Micrococcus related research and determination scheme
- 学习笔记:原码, 反码, 补码
- 重发布基础与配置
- Programming basic environment variable setting of in-house SOC
- Make and makefile summary II
- Detailed explanation of redis6.x configuration parameters
- Summary after reading "poor dad and rich dad"
- P3166 number triangle (tolerance and exclusion +gcd)
猜你喜欢

Go operation excel library excel use

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

Implementation of recommendation system collaborative filtering in spark

The e-commerce project is written in the resume. How to answer it during the interview

3、 Pinda general permission system__ pd-tools-swagger2

保护系统日志服务器和设备

【LeetCode】32、 最长有效括号

Worthington木瓜蛋白酶丨从纯化的蛋白聚糖生产糖肽(附文献)

pt-onnx-ncnn转换的问题记录(接yolov5训练)

After reading this article, you should thoroughly understand how to do interface testing
随机推荐
Republishing foundation and configuration
劳驾问一下各位老师 oracle 到pg cdc oracle 那边字段大写 pg 这边小写 同
proto转换Dart | 项目使用Protobuf | flutter 使用grpc
达梦数据库表导入导出按钮灰色,导入不了dmp文件
[Android development IOS series] Language: swift vs kotlin
E. Split into two sets
Pt onnx ncnn conversion problem record (followed by yolov5 training)
Installing and using R in Anaconda
怎么使用宝塔面板把node全栈项目部署到服务器上
Is it safe to buy funds in stock accounts? Professional answers
【Verilog数字系统设计(夏宇闻)4-----Verilog语法的基本概念2】
DQN Pytorch示例
Overview of database stress testing methods
【Verilog数字系统设计(夏宇闻)3-----Verilog语法的基本概念1】
leetcode/只出现一次的数字
我mysql to mysql数据表同步,代码上只有写在第一个顺序上的生效 其余的不生效,这个可能是
Why does the debugger display the wrong function
vite 本地运行首次进入页面加载慢问题
Proto conversion dart | project uses protobuf | fluent uses grpc
Worthington papain - production of glycopeptides from purified proteoglycans (attached Literature)