Rails 3, locking tables on an auction type engine

做~自己de王妃 提交于 2019-12-10 11:47:01

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/7166342/rails-3-locking-tables-on-an-auction-type-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!