diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-08-31 12:33:28 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-08-31 15:56:27 +0100 |
commit | 4a274ed1d216512ee4f8ebf850ddac11b430c77d (patch) | |
tree | 2dd5a9b9f920c0de64676782ba1843815c3fb22c | |
parent | 221571beb6b4bb7437989bdefaf421f993ab6002 (diff) | |
download | rails-4a274ed1d216512ee4f8ebf850ddac11b430c77d.tar.gz rails-4a274ed1d216512ee4f8ebf850ddac11b430c77d.tar.bz2 rails-4a274ed1d216512ee4f8ebf850ddac11b430c77d.zip |
Refactor connection handler
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb | 36 |
1 files changed, 14 insertions, 22 deletions
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 |