【发布时间】:2011-12-28 07:12:00
【问题描述】:
我希望能够在实际付款之前“预订”一个类似于飞机座位锁定一小段时间的元素。我认为最好的方法是通过数据库,最好是在 ORM 层。
这是一个例子:
ActiveRecord::Base.transaction do
bar = Bar.find(1, :lock => true)
# do my stuff
end
不过我需要一个更灵活的解决方案。
这是我想象它在概念上的工作方式:
# action1:
# put an expiring lock (30s) on an element (don't block unrelated code)
# other code
# action2 (after payment):
# come back to the locked element to claim ownership of it
更新:尝试在 Rails 中执行此操作的任何人都应首先尝试使用built-in optimistic locking 功能。
【问题讨论】:
标签: ruby-on-rails activerecord transactions locking