Pessimistic Locking plugin

Plugin details

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

Repositoryhttp://projects.netlab.jp/svn/rails_plugins/pessimistic_locking Author Shugo Maeda Tags Database, lock LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install http://projects.netlab.jp/svn/rails_plugins/pessimistic_locking

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 = accounts.detect { |account| ... }
      account2 = accounts.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.

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: hardway, 8 months ago