I'm building an auction-like system in Rails 3 (using PostgreSQL as the database).
我正在Rails 3中构建类似拍卖的系统(使用PostgreSQL作为数据库)。
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:
我担心当两个不同的用户同时点击“出价”时会发生什么(假设我至少有2个应用服务器,并且他们同时点击服务器)。我有两个可接受的行为:
- 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
其中一个“胜利”,另一个收到错误消息
其中一个“胜利”,另一个出价是下一个(每个出价加上0.01欧元的价格)。因此,重复所有并发出价直到创建
So my question is: how to handle this problem on Rails 3? I think using regular transactions isn't enough, or is it?
所以我的问题是:如何在Rails 3上处理这个问题?我认为使用常规交易还不够,或者是这样吗?
1 个解决方案
#1
3
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.
Rails悲观锁定应该可以让你实现你想要的。据我所知,这将让您处理将更新排序到数据库而不会抛出错误的问题。
Here is a thread that explains the two different types of locking better that I can: Optimistic vs. Pessimistic locking
这是一个线程,可以更好地解释两种不同类型的锁定:乐观与悲观锁定
#1
3
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.
Rails悲观锁定应该可以让你实现你想要的。据我所知,这将让您处理将更新排序到数据库而不会抛出错误的问题。
Here is a thread that explains the two different types of locking better that I can: Optimistic vs. Pessimistic locking
这是一个线程,可以更好地解释两种不同类型的锁定:乐观与悲观锁定