提交 5879b15f 编写于 作者: N Nick 提交者: Nick Sieger

Rename defined_connections to connection_pools

- Distinguis meaning of "active_connections" to always mean connections
  associated with the current thread
上级 6edaa267
......@@ -15,12 +15,16 @@ def initialize (config, adapter_method)
@@verification_timeout = 0
# The class -> connection pool map
@@defined_connections = {}
@@connection_pools = {}
class << self
# for internal use only
def active_connections
@@defined_connections.inject([]) {|arr,kv| arr << kv.last.active_connection}.compact.uniq
@@connection_pools.inject({}) do |hash,kv|
hash[kv.first] = kv.last.active_connection
hash.delete(kv.first) unless hash[kv.first]
hash
end
end
# Returns the connection currently associated with the class. This can
......@@ -32,21 +36,27 @@ def connection
# Clears the cache which maps classes to connections.
def clear_active_connections!
clear_cache!(@@defined_connections) do |name, pool|
pool.disconnect!
clear_cache!(@@connection_pools) do |name, pool|
pool.clear_active_connections!
end
end
# Clears the cache which maps classes
def clear_reloadable_connections!
clear_cache!(@@defined_connections) do |name, pool|
clear_cache!(@@connection_pools) do |name, pool|
pool.clear_reloadable_connections!
end
end
def clear_all_connections!
clear_cache!(@@connection_pools) do |name, pool|
pool.disconnect!
end
end
# Verify active connections.
def verify_active_connections! #:nodoc:
@@defined_connections.each_value {|pool| pool.verify_active_connections!}
@@connection_pools.each_value {|pool| pool.verify_active_connections!}
end
private
......@@ -97,7 +107,7 @@ def self.establish_connection(spec = nil)
raise AdapterNotSpecified unless defined? RAILS_ENV
establish_connection(RAILS_ENV)
when ConnectionSpecification
@@defined_connections[name] = ConnectionAdapters::ConnectionPool.new(spec)
@@connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec)
when Symbol, String
if configuration = configurations[spec.to_s]
establish_connection(configuration)
......@@ -140,7 +150,7 @@ def self.retrieve_connection #:nodoc:
end
def self.retrieve_connection_pool
pool = @@defined_connections[name]
pool = @@connection_pools[name]
return pool if pool
return nil if ActiveRecord::Base == self
superclass.retrieve_connection_pool
......@@ -156,8 +166,8 @@ def self.connected?
# can be used as an argument for establish_connection, for easily
# re-establishing the connection.
def self.remove_connection(klass=self)
pool = @@defined_connections[klass.name]
@@defined_connections.delete_if { |key, value| value == pool }
pool = @@connection_pools[klass.name]
@@connection_pools.delete_if { |key, value| value == pool }
pool.disconnect! if pool
pool.spec.config if pool
end
......
......@@ -26,7 +26,7 @@ def gather_connections
5.times do
Thread.new do
Topic.find :first
@connections << ActiveRecord::Base.active_connections.first
@connections << ActiveRecord::Base.active_connections.values.first
end.join
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册