Permalink
Browse files

Make connection pool retrieval faster

* Loop rather than recurse in retrieve_connection_pool
* Key the hash by class rather than class name. This avoids creating
  unnecessary strings.
  • Loading branch information...
1 parent ae79c7a commit 221571beb6b4bb7437989bdefaf421f993ab6002 @jonleighton jonleighton committed Aug 31, 2012
@@ -505,9 +505,9 @@ def connection_pools
@connection_pools[Process.pid]
end
- def establish_connection(name, spec)
+ def establish_connection(klass, spec)
set_pool_for_spec spec, ConnectionAdapters::ConnectionPool.new(spec)
- set_class_to_pool name, connection_pools[spec]
+ set_class_to_pool klass, connection_pools[spec]
end
# Returns true if there are any active connections among the connection
@@ -553,7 +553,7 @@ def connected?(klass)
# can be used as an argument for establish_connection, for easily
# re-establishing the connection.
def remove_connection(klass)
- pool = class_to_pool.delete(klass.name)
+ pool = class_to_pool.delete(klass)
return nil unless pool
connection_pools.delete pool.spec
@@ -563,12 +563,15 @@ def remove_connection(klass)
end
def retrieve_connection_pool(klass)
- if !(klass < Model::Tag)
- get_pool_for_class('ActiveRecord::Model') # default connection
- else
- pool = get_pool_for_class(klass.name)
- pool || retrieve_connection_pool(klass.superclass)
+ pool = get_pool_for_class(klass)
+
+ until pool
+ klass = klass.superclass
+ break unless klass < Model::Tag
+ pool = get_pool_for_class(klass)
end
+
+ pool || get_pool_for_class(ActiveRecord::Model)
end
private
@@ -581,8 +584,8 @@ def set_pool_for_spec(spec, pool)
@connection_pools[Process.pid][spec] = pool
end
- def set_class_to_pool(name, pool)
- @class_to_pool[Process.pid][name] = pool
+ def set_class_to_pool(klass, pool)
+ @class_to_pool[Process.pid][klass] = pool
pool
end
@@ -44,7 +44,7 @@ def establish_connection(spec = ENV["DATABASE_URL"])
end
remove_connection
- connection_handler.establish_connection name, spec
+ connection_handler.establish_connection self, spec
end
# Returns the connection currently associated with the class. This can
@@ -4,16 +4,11 @@ module ActiveRecord
module ConnectionAdapters
class ConnectionHandlerTest < ActiveRecord::TestCase
def setup
+ @klass = Class.new { include Model::Tag }
+ @subklass = Class.new(@klass) { include Model::Tag }
+
@handler = ConnectionHandler.new
- @handler.establish_connection 'america', Base.connection_pool.spec
- @klass = Class.new do
- include Model::Tag
- def self.name; 'america'; end
- end
- @subklass = Class.new(@klass) do
- include Model::Tag
- def self.name; 'north america'; end
- end
+ @handler.establish_connection @klass, Base.connection_pool.spec
end
def test_retrieve_connection

0 comments on commit 221571b

Please sign in to comment.