当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Why is Schnorr Signature known as the biggest technology update after bitcoin segwit
- AQS解析
- 喜获蚂蚁offer,定级p7,面经分享,万字长文带你走完面试全过程
- 【Python 1-6】Python教程之——数字
- 华为在5G手机市场占据绝对优势,市调机构对小米的市占出现分歧
- AI周报:允许“员工自愿降薪”;公司回应:员工内心高兴满意;虎牙HR将员工抬出公司;瑞典禁用华为中兴5G设备
- On the confirmation of original data assets
- 我用 Python 找出了删除我微信的所有人并将他们自动化删除了
- 原创 | 数据资产确权浅议
- 优化if-else代码的八种方案
猜你喜欢
“他,程序猿,35岁,被劝退”:不要只懂代码,会说话,胜过10倍默默努力
用 Python 写出来的进度条,竟如此美妙~
B站stm32视频学习
软件开发中如何与人协作? | 每日趣闻
模板引擎的整理归纳
Bccoin tells you: what is the most reliable investment project at the end of the year!
10个常见的软件架构模式
Flink从入门到真香(3、从集合和文件中读取数据)
Millet and oppo continue to soar in the European market, and Xiaomi is even closer to apple
你的云服务器可以用来做什么?云服务器有什么用途?
随机推荐
RestfulApi 学习笔记——父子资源(四)
Alibaba cloud accelerates its growth and further consolidates its leading edge
小青台正式踏上不归路的第3天
On the confirmation of original data assets
Blockchain weekly: the development of digital currency is written into the 14th five year plan; Biden invited senior adviser of MIT digital currency program to join the presidential transition team; V
Bccoin tells you: what is the most reliable investment project at the end of the year!
How to cooperate with people in software development? |Daily anecdotes
LeanCloud 十月变化
PMP考试通过心得分享
Comics: looking for the best time to buy and sell stocks
Tidb performance competition 11.02-11.06
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
From a friend recently Ali, Tencent, meituan and other P7 Python development post interview questions
GopherChina 2020大会
10 common software architecture patterns
PMP experience sharing
What is SVG?
Windows下快递投递柜、寄存柜的软件初探
Essential for back-end programmers: distributed transaction Basics