diff --git a/lib/odba/persistable.rb b/lib/odba/persistable.rb index f04ba7c..83064c6 100644 --- a/lib/odba/persistable.rb +++ b/lib/odba/persistable.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby # encoding: utf-8 -# ODBA::Persistable -- odba -- 23.12.2011 -- mhatakeyama@ywesee.com +# ODBA::Persistable -- odba -- 09.03.2012 -- mhatakeyama@ywesee.com # ODBA::Persistable -- odba -- 29.04.2004 -- hwyss@ywesee.com rwaltert@ywesee.com mwalder@ywesee.com class Object # :nodoc: all @@ -22,6 +22,7 @@ require 'observer' module ODBA class Stub; end module Persistable + @@persistable_mutex = Mutex.new meta = Struct.new(:exact, :limit) Exact = meta.new Exact.exact = true @@ -401,23 +402,25 @@ module ODBA def odba_stubize(obj, opts={}) # :nodoc: return false if(frozen?) id = obj.odba_id - odba_potentials.each { |name| - var = instance_variable_get(name) - # must not be synchronized because of the following if - # statement (if an object has already been replaced by - # a stub, it will have the correct id and it - # will be ignored) - case var - when Stub - # no need to make a new stub - when Persistable - if(var.odba_id == id) - stub = ODBA::Stub.new(id, self, obj) - instance_variable_set(name, stub) + @@persistable_mutex.synchronize do + odba_potentials.each { |name| + var = instance_variable_get(name) + # must not be synchronized because of the following if + # statement (if an object has already been replaced by + # a stub, it will have the correct id and it + # will be ignored) + case var + when Stub + # no need to make a new stub + when Persistable + if(var.odba_id == id) + stub = ODBA::Stub.new(id, self, obj) + instance_variable_set(name, stub) + end end - end - } - odba_notify_observers(:stubize, id, obj.object_id) + } + odba_notify_observers(:stubize, id, obj.object_id) + end ## allow CacheEntry to retire true end @@ -527,20 +530,22 @@ class Array # :nodoc: all end def odba_stubize(obj, opts={}) # :nodoc: return false if(frozen?) - super - if opts[:force] - id = obj.odba_id - collect! do |item| - if item.is_a?(ODBA::Persistable) \ - && !item.is_a?(ODBA::Stub) && item.odba_id == id - ODBA::Stub.new(id, self, obj) - else - item + @@persistable_mutex.synchronize do + super + if opts[:force] + id = obj.odba_id + collect! do |item| + if item.is_a?(ODBA::Persistable) \ + && !item.is_a?(ODBA::Stub) && item.odba_id == id + ODBA::Stub.new(id, self, obj) + else + item + end end + true + else + false end - true - else - false end end def odba_target_ids @@ -604,17 +609,19 @@ class Hash # :nodoc: all end def odba_stubize(obj, opts={}) # :nodoc: return false if(frozen?) - super - if opts[:force] - dup = {} - each do |pair| - pair.odba_stubize(obj) - dup.store *pair + @@persistable_mutex.synchronize do + super + if opts[:force] + dup = {} + each do |pair| + pair.odba_stubize(obj) + dup.store *pair + end + replace dup + true + else + false end - replace dup - true - else - false end end def odba_target_ids