Rails 3,拍卖类型引擎上的锁定表
我正在 Rails 3 中构建一个类似拍卖的系统(使用 PostgreSQL 作为数据库)。
想象一下您有一个产品。一个产品有多个出价。
我担心当 2 个不同的用户同时单击“出价”时会发生什么(假设我至少有 2 个应用程序服务器,并且它们同时访问服务器)。我对此有两种可能的可接受行为:
- 其中一个“获胜”,另一个收到错误消息,
- 其中一个“获胜”,然后创建另一个出价(每个出价在价格上增加 0.01 欧元)。所以所有并发的出价都会重复直到创建
所以我的问题是:如何在 Rails 3 上处理这个问题?我认为使用常规交易还不够,不是吗?
I'm building an auction-like system in Rails 3 (using PostgreSQL as the database).
Imagine you have a Product. A product has many Bids.
I'm worried with what happens when 2 different users click "Bid" at the same time (providing I have at least 2 application servers, and they hit the servers at the same time). I have two possible acceptable behaviors for this:
- one of them "wins", and the other receives an error message
- one of them "wins", and the other bid is created next (each bid adds 0.01€ to the price). So all the concurrent bids are repeated until created
So my question is: how to handle this problem on Rails 3? I think using regular transactions isn't enough, or is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails 悲观锁定应该可以让你实现你想要的。据我所知,这可以让您处理订购数据库更新的问题,而不会引发错误。
这是一个线程,我可以更好地解释两种不同类型的锁定:乐观锁定与悲观锁定
Rails pessimistic locking should allow you to achieve what you want. As far as I am aware this would let you handle the issue of ordering your updates to the database without throwing errors.
Here is a thread that explains the two different types of locking better that I can: Optimistic vs. Pessimistic locking