Thursday 9 April 2009

More 2.3.2 Upgrade issues

Lots of ActiveRecord::StaleObjectError errors

Ok took some time clearing these. The basic problem was with large complex trees of objects resulting in multiple paths destroying the same objects. For example a 3 level process <-- context <-- parameters. As a handy shortcut process has a collection of parameters and contexts. The delete was cascading down via both process->context->parameter and process->parameter. This resulted in a attempt to delete a record twice.

Methods of fixing these problems:-
  • look carefully and change see if :delete_all raw delete can be used for dependent records.
  • look a collection and see if :destroy options can be removed
  • monkey patch to activerecord/lib/active_record/locking/optimistic.rb line 126 so only errors if real problem with stale record, allow duplicate deletes.
My little patch of Active Record to the last few problems as the following:-

unless affected_rows == 1
raise ActiveRecord::StaleObjectError, "Attempted to delete a stale object"
end


Changed to add test for already delete record and say which object actually had the problem :-

if affected_rows != 1 and self.class.exists?(self.id)
raise ActiveRecord::StaleObjectError, "Attempted to delete a stale object #{self.class}[#{self.id}]"
end

1 comment:

Unknown said...

try (sorry cannot indent because i cannot use pre tag)

before_destroy :disable_optimistic_locking
def disable_optimistic_locking
self.instance_eval do
def locking_enabled?
false
end
end
end