From 4a274ed1d216512ee4f8ebf850ddac11b430c77d Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 31 Aug 2012 12:33:28 +0100 Subject: Refactor connection handler --- .../abstract/connection_pool.rb | 36 +++++++++------------- 1 file changed, 14 insertions(+), 22 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index 1f731036c9..ed8f274cdb 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -506,8 +506,8 @@ module ActiveRecord end def establish_connection(klass, spec) - set_pool_for_spec spec, ConnectionAdapters::ConnectionPool.new(spec) - set_class_to_pool klass, connection_pools[spec] + class_to_pool[klass] = + connection_pools[spec] = ConnectionAdapters::ConnectionPool.new(spec) end # Returns true if there are any active connections among the connection @@ -580,31 +580,23 @@ module ActiveRecord @class_to_pool[Process.pid] end - def set_pool_for_spec(spec, pool) - @connection_pools[Process.pid][spec] = pool - end - - def set_class_to_pool(klass, pool) - @class_to_pool[Process.pid][klass] = pool - pool - end - def get_pool_for_class(klass) - @class_to_pool[Process.pid].fetch(klass) { - c_to_p = @class_to_pool.values.find { |class_to_pool| - class_to_pool[klass] - } - - if c_to_p - pool = c_to_p[klass] - pool = ConnectionAdapters::ConnectionPool.new pool.spec - set_pool_for_spec pool.spec, pool - set_class_to_pool klass, pool + class_to_pool.fetch(klass) { + if ancestor_pool = get_pool_for_class_from_any_process(klass) + # A connection was established in an ancestor process that must have + # subsequently forked. We can't reuse the connection, but we can copy + # the specification and establish a new connection with it. + establish_connection klass, ancestor_pool.spec else - set_class_to_pool klass, nil + class_to_pool[klass] = nil end } end + + def get_pool_for_class_from_any_process(klass) + c_to_p = @class_to_pool.values.find { |class_to_pool| class_to_pool[klass] } + c_to_p && c_to_p[klass] + end end class ConnectionManagement -- cgit v1.2.3