当前位置:网站首页>On the concurrency of update operation
On the concurrency of update operation
2020-11-08 13:52:00 【osc_7eqzxl4g】
In a high concurrency scenario , This is often the case :
A Please come here , Look up a piece of data , Conduct update operation , meanwhile B Please come here at this time , Query this data , And operate . And then there will be B stay A After that, the query operation will be carried out , But the actual B But the data is A Cover .
Table name A, Field called number, As follows SQL sentence :
A operation sentence 1:select num from store where id='1';
Suppose that at this point a gets num= 99
B operation sentence 2:select num from store where id='1';
Because Party A has not yet update operation , B also got num= 99
Now A Conduct update operation
update store set num =${num} +1 where id='1';
At this time, it is written to the database num That is to say 100
here B The request also initiated the update operation :
update store set num =${num} +1 where id='1';
Our expectation at this time should have been 101 Of , But actually B I wrote in the database again 100
Solution :
(1) Introduce the concept of a version number , In the table A Add one version Field
A operation sentence 1:select num,version from store where id='1';
Suppose that at this point a gets num= 99 version =1
B operation sentence 2:select num,version from store where id='1';
Because Party A has not yet update operation , B also got num= 99 version=1
Now A Conduct update operation
update store set num =${num} +1 where id='1' and version = ${version};
At this time, it is written to the database num That is to say 100, version =2
here B The request also initiated the update operation :
update store set num =${num} +1 where id='1' and version = ${version} ;
At this time, we found the condition version = 1 Don't set up , Because the last step is version Have been to 2 了 , therefore update Cannot update .
(2) Solution :update A set number=number+1 where id=1; Statements are processed directly
Table name A, Field called number, As follows SQL sentence :
sentence 1:update A set number=number+1 where id=1;
sentence 2:update A set number=number+2 where id=1;
Suppose these two SQL The statement is simultaneously mysql perform ,id=1 In the record number The original value of the field is 99, So is it possible that :
sentence 1 and 2 Because at the same time , They got number The values are all 99, It's all in 10 On the basis of 1 and 2, Lead to the final number Is updated to 100 or 101, instead of 102
This is actually Relational database itself needs to solve the problem . First , They were also MySQL perform , What you mean is that they are executed concurrently , The concurrent transaction is supported by special theory in relational database - ACID, The theory of transaction parallelism , All relational databases implement , Include Oracle, MySQL We need to follow this principle .
The simple understanding is the principle of lock . This is the first time update Will hold id=1 This line records Exclusive lock , the second update You need to have an exclusive lock on this record to modify it , Normal word , the second update It will block , Until the first one update Submit successfully , He'll get the lock , To modify the data .
in other words , According to the theory of relational database , these two items. update If they all succeed ,id=1 Of number It must be modified to 22. If not 22, That's a serious problem with database implementation bug.
版权声明
本文为[osc_7eqzxl4g]所创,转载请带上原文链接,感谢
边栏推荐
- 优化if-else代码的八种方案
- rabbitmq(一)-基础入门
- 区块链周报:数字货币发展写入十四五规划;拜登邀请MIT数字货币计划高级顾问加入总统过渡团队;委内瑞拉推出国营加密交易所
- STM32CubeIDE下载安装-GPIO基本配置操作-Debug调试(基于CMSIS DAP Debug)
- 10个常见的软件架构模式
- The first open source Chinese Bert pre training model in the financial field
- 供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
- Interpretation of deepmind's latest paper: the causal reasoning algorithm in discrete probability tree is proposed for the first time
- Ubuntu20.04下访问FTP服务器乱码问题+上传文件
- 当Kubernetes遇到机密计算,看阿里巴巴如何保护容器内数据的安全!(附网盘链接)
猜你喜欢

喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以多少汽水

三、函数的参数

金融领域首个开源中文BERT预训练模型,熵简科技推出FinBERT 1.0

【Python 1-6】Python教程之——数字

一文剖析2020年最火十大物联网应用|IoT Analytics 年度重磅报告出炉!

阿里云视频云技术专家 LVS 演讲全文:《“云端一体”的智能媒体生产制作演进之路》

小青台正式踏上不归路的第3天

Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire

Huawei has an absolute advantage in the 5g mobile phone market, and the market share of Xiaomi is divided by the market survey organization

Implementation of verification code recognition in Python opencv pytesseract
随机推荐
How to solve the difference between NAT IP and port IP
Share the experience of passing the PMP examination
一文剖析2020年最火十大物联网应用|IoT Analytics 年度重磅报告出炉!
OR Talk NO.19 | Facebook田渊栋博士:基于蒙特卡洛树搜索的隐动作集黑盒优化 - 知乎
AQS解析
后端程序员必备:分布式事务基础篇
2035 we will build such a country
基于阿里云日志服务快速打造简版业务监控看板
What is the database paradigm
华为云重大变革:Cloud&AI 升至华为第四大 BG ,火力全开
Golang ICMP协议探测存活主机
Bccoin tells you: what is the most reliable investment project at the end of the year!
重返全球第三,小米做对了什么?
一文读懂机器学习“数据中毒”
Flink from introduction to Zhenxiang (10. Sink data output elasticsearch)
How to write a resume and project
Huawei has an absolute advantage in the 5g mobile phone market, and the market share of Xiaomi is divided by the market survey organization
Entry level! Teach you how to develop small programs without asking for help (with internet disk link)
别再在finally里面释放资源了,解锁个新姿势!
Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search