Mysql(innodb)行级锁定是解决方案吗?

发布于 2024-09-29 17:52:46 字数 468 浏览 3 评论 0原文

最近将我的 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
    points

  • Records 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2024-10-06 17:52:46

不。行级锁定与表级锁定主要是一个性能问题。

您正在寻找的是交易。开始交易,增加积分等级,添加奖励,提交。要么整个​​交易完成,奖励和积分同时完成,要么失败,两者都不会发生。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文