| 2 | | # Active Records support optimistic locking if the field <tt>lock_version</tt> is present. Each update to the |
|---|
| 3 | | # record increments the lock_version column and the locking facilities ensure that records instantiated twice |
|---|
| 4 | | # will let the last one saved raise a StaleObjectError if the first was also updated. Example: |
|---|
| 5 | | # |
|---|
| 6 | | # p1 = Person.find(1) |
|---|
| 7 | | # p2 = Person.find(1) |
|---|
| 8 | | # |
|---|
| 9 | | # p1.first_name = "Michael" |
|---|
| 10 | | # p1.save |
|---|
| 11 | | # |
|---|
| 12 | | # p2.first_name = "should fail" |
|---|
| 13 | | # p2.save # Raises a ActiveRecord::StaleObjectError |
|---|
| 14 | | # |
|---|
| 15 | | # You're then responsible for dealing with the conflict by rescuing the exception and either rolling back, merging, |
|---|
| 16 | | # or otherwise apply the business logic needed to resolve the conflict. |
|---|
| 17 | | # |
|---|
| 18 | | # You must ensure that your database schema defaults the lock_version column to 0. |
|---|
| 19 | | # |
|---|
| 20 | | # This behavior can be turned off by setting <tt>ActiveRecord::Base.lock_optimistically = false</tt>. |
|---|
| 21 | | # To override the name of the lock_version column, invoke the <tt>set_locking_column</tt> method. |
|---|
| 22 | | # This method uses the same syntax as <tt>set_table_name</tt> |
|---|
| 24 | | def self.included(base) #:nodoc: |
|---|
| 25 | | base.class_eval do |
|---|
| 26 | | alias_method_chain :update, :lock |
|---|
| | 3 | # Active Records support optimistic locking if the field <tt>lock_version</tt> is present. Each update to the |
|---|
| | 4 | # record increments the lock_version column and the locking facilities ensure that records instantiated twice |
|---|
| | 5 | # will let the last one saved raise a StaleObjectError if the first was also updated. Example: |
|---|
| | 6 | # |
|---|
| | 7 | # p1 = Person.find(1) |
|---|
| | 8 | # p2 = Person.find(1) |
|---|
| | 9 | # |
|---|
| | 10 | # p1.first_name = "Michael" |
|---|
| | 11 | # p1.save |
|---|
| | 12 | # |
|---|
| | 13 | # p2.first_name = "should fail" |
|---|
| | 14 | # p2.save # Raises a ActiveRecord::StaleObjectError |
|---|
| | 15 | # |
|---|
| | 16 | # You're then responsible for dealing with the conflict by rescuing the exception and either rolling back, merging, |
|---|
| | 17 | # or otherwise apply the business logic needed to resolve the conflict. |
|---|
| | 18 | # |
|---|
| | 19 | # You must ensure that your database schema defaults the lock_version column to 0. |
|---|
| | 20 | # |
|---|
| | 21 | # This behavior can be turned off by setting <tt>ActiveRecord::Base.lock_optimistically = false</tt>. |
|---|
| | 22 | # To override the name of the lock_version column, invoke the <tt>set_locking_column</tt> method. |
|---|
| | 23 | # This method uses the same syntax as <tt>set_table_name</tt> |
|---|
| | 24 | module Optimistic |
|---|
| | 25 | def self.included(base) #:nodoc: |
|---|
| | 26 | super |
|---|
| | 27 | base.extend ClassMethods |
|---|
| | 28 | |
|---|
| | 29 | base.cattr_accessor :lock_optimistically |
|---|
| | 30 | base.lock_optimistically = true |
|---|
| | 31 | |
|---|
| | 32 | base.alias_method_chain :update, :lock |
|---|
| | 33 | class << base |
|---|
| | 34 | alias_method :locking_column=, :set_locking_column |
|---|
| | 35 | end |
|---|
| | 36 | end |
|---|
| | 37 | |
|---|
| | 38 | def locking_enabled? #:nodoc: |
|---|
| | 39 | lock_optimistically && respond_to?(self.class.locking_column) |
|---|
| | 40 | end |
|---|
| | 41 | |
|---|
| | 42 | def update_with_lock #:nodoc: |
|---|
| | 43 | return update_without_lock unless locking_enabled? |
|---|
| | 44 | |
|---|
| | 45 | lock_col = self.class.locking_column |
|---|
| | 46 | previous_value = send(lock_col) |
|---|
| | 47 | send(lock_col + '=', previous_value + 1) |
|---|
| | 48 | |
|---|
| | 49 | affected_rows = connection.update(<<-end_sql, "#{self.class.name} Update with optimistic locking") |
|---|
| | 50 | UPDATE #{self.class.table_name} |
|---|
| | 51 | SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} |
|---|
| | 52 | WHERE #{self.class.primary_key} = #{quote(id)} |
|---|
| | 53 | AND #{lock_col} = #{quote(previous_value)} |
|---|
| | 54 | end_sql |
|---|
| | 55 | |
|---|
| | 56 | unless affected_rows == 1 |
|---|
| | 57 | raise ActiveRecord::StaleObjectError, "Attempted to update a stale object" |
|---|
| | 58 | end |
|---|
| | 59 | |
|---|
| | 60 | return true |
|---|
| | 61 | end |
|---|
| | 62 | |
|---|
| | 63 | module ClassMethods |
|---|
| | 64 | DEFAULT_LOCKING_COLUMN = 'lock_version' |
|---|
| | 65 | |
|---|
| | 66 | # Set the column to use for optimistic locking. Defaults to lock_version. |
|---|
| | 67 | def set_locking_column(value = nil, &block) |
|---|
| | 68 | define_attr_method :locking_column, value, &block |
|---|
| | 69 | value |
|---|
| | 70 | end |
|---|
| | 71 | |
|---|
| | 72 | # The version column used for optimistic locking. Defaults to lock_version. |
|---|
| | 73 | def locking_column |
|---|
| | 74 | reset_locking_column |
|---|
| | 75 | end |
|---|
| | 76 | |
|---|
| | 77 | # Reset the column used for optimistic locking back to the lock_version default. |
|---|
| | 78 | def reset_locking_column |
|---|
| | 79 | set_locking_column DEFAULT_LOCKING_COLUMN |
|---|
| | 80 | end |
|---|
| 29 | | |
|---|
| 30 | | def update_with_lock #:nodoc: |
|---|
| 31 | | return update_without_lock unless locking_enabled? |
|---|
| 32 | | |
|---|
| 33 | | lock_col = self.class.locking_column |
|---|
| 34 | | previous_value = send(lock_col) |
|---|
| 35 | | send(lock_col + '=', previous_value + 1) |
|---|
| 36 | | |
|---|
| 37 | | affected_rows = connection.update(<<-end_sql, "#{self.class.name} Update with optimistic locking") |
|---|
| 38 | | UPDATE #{self.class.table_name} |
|---|
| 39 | | SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} |
|---|
| 40 | | WHERE #{self.class.primary_key} = #{quote(id)} |
|---|
| 41 | | AND #{lock_col} = #{quote(previous_value)} |
|---|
| 42 | | end_sql |
|---|
| 43 | | |
|---|
| 44 | | unless affected_rows == 1 |
|---|
| 45 | | raise ActiveRecord::StaleObjectError, "Attempted to update a stale object" |
|---|
| 46 | | end |
|---|
| 47 | | |
|---|
| 48 | | return true |
|---|
| 49 | | end |
|---|
| 50 | | end |
|---|
| 51 | | |
|---|
| 52 | | class Base |
|---|
| 53 | | @@lock_optimistically = true |
|---|
| 54 | | cattr_accessor :lock_optimistically |
|---|
| 55 | | |
|---|
| 56 | | def locking_enabled? #:nodoc: |
|---|
| 57 | | lock_optimistically && respond_to?(self.class.locking_column) |
|---|
| 58 | | end |
|---|
| 59 | | |
|---|
| 60 | | class << self |
|---|
| 61 | | def set_locking_column(value = nil, &block) |
|---|
| 62 | | define_attr_method :locking_column, value, &block |
|---|
| 63 | | end |
|---|
| 64 | | |
|---|
| 65 | | def locking_column #:nodoc: |
|---|
| 66 | | reset_locking_column |
|---|
| 67 | | end |
|---|
| 68 | | |
|---|
| 69 | | def reset_locking_column #:nodoc: |
|---|
| 70 | | default = 'lock_version' |
|---|
| 71 | | set_locking_column(default) |
|---|
| 72 | | default |
|---|
| 73 | | end |
|---|
| 74 | | end |
|---|
| 75 | | |
|---|