轨道3,对拍卖式发动机锁表 [英] Rails 3, locking tables on an auction type engine
问题描述
我要建一个拍卖样系统的Rails 3(使用PostgreSQL作为数据库)。
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.
我担心什么发生在2个不同的用户点击投标在同一时间(提供我至少有2个应用程序服务器和他们打在同一时间服务器)。我有两个可能的可以接受的行为对这样的:
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:
- 其中之一胜,而另一个接收的错误消息
- 在其中一个赢,另一个出价接下来创建(每次出价增加0.01€的价格)。因此,所有的并行出价重复,直到创建
所以我的问题是:如何处理Rails 3中这个问题?我想到了用常规交易是不够的,是这样吗?
So my question is: how to handle this problem on Rails 3? I think using regular transactions isn't enough, or is it?
推荐答案
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
这篇关于轨道3,对拍卖式发动机锁表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!