Mysql(innodb)行级锁定是解决方案吗?
最近将我的 codeiginter 应用程序放入集群环境后遇到了问题。用户能够获得积分级别的网站奖励,在他们单击“提交”以检索奖励后,我有代码可以获取奖励的当前积分级别,并将其保存在包含 user_id 和他们获得奖励的积分级别的表中在。假设在用户获得奖励后将该奖励增加 1 点。我遇到了一个问题,2 个用户可能同时单击“提交”,并且在记录表中,两个用户都获得了相同积分级别的奖励,这是不应该发生的,因为它应该在每次之后增加 1 个积分提交。对正在递增的奖励行进行行级锁定会阻止此问题吗?我该如何实施它?
如果您需要更多信息或澄清,请告诉我。
用户表
编号
用户名
[...]奖励表
编号
姓名
积分记录表
编号
用户 ID
奖励_id
点
应该是用户提交检索奖励时奖励的积分数量。
Recently ran into a problem after putting my codeiginter application into a clustered environment. Users are able to get site rewards at point levels, after they click submit to retrieve the reward I have code that grabs the current point level of the reward is at and save that in a table with the user_id and what point level they got the reward at. it is suppose to increment that reward by 1 point level after the user has retrieved the reward. I have ran into a problem where 2 users might click submit at the same time and in the records table it has that both users got the reward at the same point level which shouldn't happen because it is suppose to increment by 1 point after each submit. would row level locking on the reward row that is being incrementing stop this issue ? and how do i implement it?
If you need anymore information or clarification let me know.
Users Table
id
username
[...]Rewards Table
id
name
pointsRecords Table
id
users_id
rewards_id
points
The points in the records table is suppose to be the amount of points the reward was at when the user submitted to retrieve the reward.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。行级锁定与表级锁定主要是一个性能问题。
您正在寻找的是交易。开始交易,增加积分等级,添加奖励,提交。要么整个交易完成,奖励和积分同时完成,要么失败,两者都不会发生。
No. Row-level vs table-level locking is primarily a performance issue.
What you're looking for is transactions. Start the transaction, increase the point level, add the reward, commit. Either the whole transaction completes and both the reward and the point-increase are done at once, or it fails and neither happens.