当前位置:网站首页>关于update操作并发问题
关于update操作并发问题
2020-11-08 13:52:00 【osc_7eqzxl4g】
在高并发的场景下,经常会遇到这种情况:
A请求过来,查询出来一条数据,进行update操作,与此同时B请求也在这个时候过来,对这条数据进行查询,并进行操作。此时就会出现B在A之后进行查询操作,但是实际B的数据却被A覆盖。
表名A,字段名为 number,如下的SQL语句:
甲操作 语句1:select num from store where id='1';
假设此时甲获取到 num= 99
乙操作 语句2:select num from store where id='1';
因为甲方还没有update操作,乙获也取到 num= 99
这时候A进行update操作
update store set num =${num} +1 where id='1';
这时候写入数据库的num即为100
此时B请求也发起了更新操作:
update store set num =${num} +1 where id='1';
这时候我们的预期本应该是101的,但是实际上B又在数据库写入了100
解决方案:
(1)引入一个版本号的概念,在表A中增加一个version字段
甲操作 语句1:select num,version from store where id='1';
假设此时甲获取到 num= 99 version =1
乙操作 语句2:select num,version from store where id='1';
因为甲方还没有update操作,乙获也取到 num= 99 version=1
这时候A进行update操作
update store set num =${num} +1 where id='1' and version = ${version};
这时候写入数据库的num即为100, version =2
此时B请求也发起了更新操作:
update store set num =${num} +1 where id='1' and version = ${version} ;
这时候发现条件version = 1不成立,因为上一步操作version已经为2了,所以update无法更新。
(2)解决方式:update A set number=number+1 where id=1; 语句直接处理
表名A,字段名为 number,如下的SQL语句:
语句1:update A set number=number+1 where id=1;
语句2:update A set number=number+2 where id=1;
假设这两条SQL语句同时被mysql执行,id=1的记录中number字段的原始值为99,那么是否有可能出现这种情况:
语句1和2因为同时执行,他们得到的number的值都是99,都是在10的基础上分别加1和2,导致最终number被更新为100或101,而不是102
这个其实就是 关系型数据库本身就需要解决的问题。首先,他们同时被MySQL执行,你的意思其实就是他们是并发执行的,而并发执行的事务在关系型数据库中是有专门的理论支持的- ACID,事务并行等理论,所有关系型数据库实现,包括Oracle, MySQL都需要遵循这个原理。
简单一点理解就是锁的原理。这个时候第一个update会持有id=1这行记录的 排它锁,第二个update需要持有这个记录的排它锁的才能对他进行修改,正常的话, 第二个update会阻塞,直到第一个update提交成功,他才会获得这个锁,从而对数据进行修改。
也就是说,按照关系型数据库的理论,这两个update都成功的话,id=1的number一定会被修改成22。如果不是22, 那就是数据库实现的一个严重的bug。
版权声明
本文为[osc_7eqzxl4g]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4260217/blog/4708176
边栏推荐
- How to write a resume and project
- OR Talk NO.19 | Facebook田渊栋博士:基于蒙特卡洛树搜索的隐动作集黑盒优化 - 知乎
- Millet and oppo continue to soar in the European market, and Xiaomi is even closer to apple
- Powershell 使用.Net对象发送邮件
- On monotonous stack
- 软件开发中如何与人协作? | 每日趣闻
- 区块链周报:数字货币发展写入十四五规划;拜登邀请MIT数字货币计划高级顾问加入总统过渡团队;委内瑞拉推出国营加密交易所
- Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search
- Drink soda, a bottle of soda water 1 yuan, two empty bottles can change a bottle of soda, give 20 yuan, how much soda can you
- 【Python 1-6】Python教程之——数字
猜你喜欢
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
为什么 Schnorr 签名被誉为比特币 Segwit 后的最大技术更新
Flink from introduction to Zhenxiang (10. Sink data output elasticsearch)
The first open source Chinese Bert pre training model in the financial field
AQS解析
Returning to the third place in the world, what did Xiaomi do right?
Hematemesis! Alibaba Android Development Manual! (Internet disk link attached)
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Huawei has an absolute advantage in the 5g mobile phone market, and the market share of Xiaomi is divided by the market survey organization
阿里出品!视觉计算开发者系列手册(附网盘链接)
随机推荐
laravel8更新之速率限制改进
Harbor项目高手问答及赠书活动
PMP心得分享
当Kubernetes遇到机密计算,看阿里巴巴如何保护容器内数据的安全!(附网盘链接)
Rust: performance test criteria Library
别再在finally里面释放资源了,解锁个新姿势!
原创 | 数据资产确权浅议
What is SVG?
Rust : 性能测试criterion库
新型存算一体芯片诞生,利好人工智能应用~
【Python 1-6】Python教程之——数字
Suitable for C / C + + novice learning some projects, do not give me to miss!
On the software of express delivery cabinet and deposit cabinet under Windows
Bccoin tells you: what is the most reliable investment project at the end of the year!
Hematemesis! Alibaba Android Development Manual! (Internet disk link attached)
小米、OPPO在欧洲市场继续飙涨,小米更是直逼苹果
The first open source Chinese Bert pre training model in the financial field
阿里教你深入浅出玩转物联网平台!(附网盘链接)
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
三、函数的参数