Plugins - Pessimistic Locking

StarAdd to favorites

PessimisticLocking provides row-level pessimistic locking using SELECT FOR UPDATE.

You can specify the new option :lock of ActiveRecord::Base.find() to lock rows:

Account.transaction do
  shugo = Account.find(:first, :conditions => "name = 'shugo'", :lock => true)
  yuko = Account.find(:first, :conditions => "name = 'yuko'", :lock => true)
  shugo.balance -= 100
  shugo.save
  yuko.balance += 100
  yuko.save
end

Or you can also use ActiveRecord::Base#lock() instead:

Account.transaction do
  accounts = Account.find(:all, :conditions => ...)
  account1.detect { |account| ... }
  account2.detect { |account| ... }
  account1.lock
  account2.lock
  account1.balance -= 100
  account1.save
  account2.balance += 100
  account2.save
end

The latter way may be better if you don't need lock all SELECTed rows.

Shugo Maeda

http://projects.netlab.jp/svn/rails_plugins/pessimistic_locking

Rails' (MIT)

  • Currently 0.0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Model

Tags

Comments

Add a comment

Search Plugins

Query syntax

Plugins by Category

Sponsors

Rails Kits: Get Code. Get Moving.

Have a comment?