当前位置:网站首页>On the concurrency of update operation
On the concurrency of update operation
2020-11-07 20:56:00 【daydaydream】
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.
版权声明
本文为[daydaydream]所创,转载请带上原文链接,感谢
边栏推荐
- CPU瞒着内存竟干出这种事
- 【C++学习笔记】C++ 标准库 std::thread 的简单使用,一文搞定还不简单?
- Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
- Static + code block + polymorphism + exception
- The prediction accuracy of the model is as high as 94%! Using machine learning to solve the 200 billion dollar inventory problem perfectly
- laravel8更新之维护模式改进
- 动态规划——用二进制表示集合的状态压缩DP
- 尾-递
- [original] the influence of arm platform memory and cache on the real-time performance of xenomai
- 关于update操作并发问题
猜你喜欢
IDEA-项目未自动生成 .iml 文件
Improvement of maintenance mode of laravel8 update
技术总监7年自述——如何选择一家好公司
C language I blog assignment 03
WPF 关于绘图个人总结
Ubuntu下搜狗输入法的下载安装及配置
delphi10的rest.json与system.json的踩坑
关于晋升全栈工程师,从入门到放弃的神功秘籍,不点进来看一看?
Do not understand the underlying principle of database index? That's because you don't have a B tree in your heart
京淘项目day09
随机推荐
在 Amazon SageMaker 管道模式下使用 Horovod 实现多 GPU 分布式训练
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
GrowingIO 响应式编程探索和实践
Practice of Xiaoxiong school development board: real equipment access of smart street lamp sandbox experiment
C language I blog assignment 03
Recommend suicide, openai warns: gpt-3 is too risky for medical purposes
[C + + learning notes] how about the simple use of the C + + standard library STD:: thread?
看一遍就理解,图解单链表反转
laravel8更新之维护模式改进
ROS学习---远程启动ROS节点
A detailed explanation of microservice architecture
[漫谈] 软件设计的目标和途径
Git代码提交操作,以及git push提示failed to push some refs'XXX'
Web安全(四)---XSS攻击
如何应对事关业务生死的数据泄露和删改?
C language I blog assignment 03
awk实现类sql的join操作
建议患者自杀,OpenAI警告:GPT-3用于医疗目的风险太高
Web安全(三)---CSRF攻击
凯撒密码实现