Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4462

Show
Ignore:
Timestamp:
06/20/06 01:58:36 (3 years ago)
Author:
bitsweat
Message:

r4664@asus: jeremy | 2006-06-19 18:55:36 -0700
Use the #lock method to obtain a row lock on a single record. Simply reloads the record with :lock => true.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r4460 r4462  
    11*SVN* 
    22 
    3 * Row locking. Provide a locking clause with the :lock finder option or true for the default "FOR UPDATE". [Shugo Maeda] 
     3* Row locking. Provide a locking clause with the :lock finder option or true for the default "FOR UPDATE". Use the #lock method to obtain a row lock on a single reload (reloads the record with :lock => true). [Shugo Maeda] 
    44    # Obtain an exclusive lock on person 1 so we can safely increment visits. 
    55    Person.transaction do 
  • trunk/activerecord/lib/active_record.rb

    r4461 r4462  
    4747require 'active_record/acts/tree' 
    4848require 'active_record/acts/nested_set' 
    49 require 'active_record/locking' 
     49require 'active_record/locking/optimistic' 
     50require 'active_record/locking/pessimistic' 
    5051require 'active_record/migration' 
    5152require 'active_record/schema' 
     
    5657  include ActiveRecord::Validations 
    5758  include ActiveRecord::Locking::Optimistic 
     59  include ActiveRecord::Locking::Pessimistic 
    5860  include ActiveRecord::Callbacks 
    5961  include ActiveRecord::Observing 
  • trunk/activerecord/lib/active_record/base.rb

    r4460 r4462  
    15421542 
    15431543      # Reloads the attributes of this object from the database. 
    1544       def reload 
     1544      # The optional options argument is passed to find when reloading so you 
     1545      # may do e.g. record.reload(:lock => true) to reload the same record with 
     1546      # an exclusive row lock. 
     1547      def reload(options = nil) 
    15451548        clear_aggregation_cache 
    15461549        clear_association_cache 
    1547         @attributes.update(self.class.find(self.id).instance_variable_get('@attributes')) 
     1550        @attributes.update(self.class.find(self.id, options).instance_variable_get('@attributes')) 
    15481551        self 
    15491552      end 
  • trunk/activerecord/test/locking_test.rb

    r4460 r4462  
    8383  end 
    8484 
     85  # Locking a record reloads it. 
     86  def test_sane_lock_method 
     87    assert_nothing_raised do 
     88      Person.transaction do 
     89        person = Person.find 1 
     90        old, person.first_name = person.first_name, 'fooman' 
     91        person.lock! 
     92        assert_equal old, person.first_name 
     93      end 
     94    end 
     95  end 
     96 
    8597  if current_adapter?(:PostgreSQLAdapter) 
    8698    def test_no_locks_no_wait