I am using with_lock
to lock the block of instruction where I am going to call update_all
. If an exception is raised inside the block, does a locked object stay locked?
For example, validator:handle_conflict raise the exception ActiveRecord::StaleObjectError
, will with_lock
release the lock? If it doesn't release lock, how to release the lock when exception is raised?
validate :handle_conflict, only: :update_all
def set_total_and_buckets_used
begin
/////
else
self.with_lock do
self.reload
attributes["updated_at"] = Time.now.utc
Product.where(:id => self.id).update_all(attributes)
self.attributes = attributes
end
end
rescue ActiveRecord::StaleObjectError => e
if tries < MAX_RETRIES
tries += 1
sleep(1 + tries)
retry
else
raise Exception.new(timeout.inspect)
end
end
attributes
end
def handle_conflict
if self.updated_at.to_f > original_updated_at
@original_updated_at = nil
raise ActiveRecord::StaleObjectError.new("Car is changed while you were editing")
end
end
Aucun commentaire:
Enregistrer un commentaire