class ActiveRecord::ConnectionAdapters::ConnectionHandler
determine that connection pool that they should use.
ActiveRecord::Base.connection_handler. ActiveRecord models use this to
Normally there is only a single ConnectionHandler instance, accessible via
is not the same as the one used by Book/ScaryBook/GoodBook.
same connection pool. However, the connection pool used by Author/BankAccount
the same connection pool. Likewise, Author and BankAccount will use the
than the default database). Then Book, ScaryBook and GoodBook will all use
Suppose that Book is to connect to a separate database (i.e. one other
+– BankAccount
+– Author
| +– GoodBook
| +– ScaryBook
| |
+– Book
|
For example, suppose that you have 5 models, with the following hierarchy:
to different databases.
for keeping separate connection pools for ActiveRecord models that connect
ConnectionHandler is a collection of ConnectionPool objects. It is used
def clear_active_connections!
and also returns connections to the pool cached by threads that are no
Returns any connections in use by the current thread back to the pool,
def clear_active_connections! @connection_pools.each_value {|pool| pool.release_connection } end
def clear_all_connections!
def clear_all_connections! @connection_pools.each_value {|pool| pool.disconnect! } end
def clear_reloadable_connections!
def clear_reloadable_connections! @connection_pools.each_value {|pool| pool.clear_reloadable_connections! } end
def connected?(klass)
Returns true if a connection that's accessible to this class has
def connected?(klass) conn = retrieve_connection_pool(klass) conn ? conn.connected? : false end
def connection_pools
def connection_pools @connection_pools ||= {} end
def establish_connection(name, spec)
def establish_connection(name, spec) @connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec) end
def initialize(pools = {})
def initialize(pools = {}) @connection_pools = pools end
def remove_connection(klass)
can be used as an argument for establish_connection, for easily
connection and the defined connection (if they exist). The result
Remove the connection for this class. This will close the active
def remove_connection(klass) pool = @connection_pools[klass.name] @connection_pools.delete_if { |key, value| value == pool } pool.disconnect! if pool pool.spec.config if pool end
def retrieve_connection(klass) #:nodoc:
for (not necessarily the current class).
opened and set as the active connection for the class it was defined
active or defined connection: if it is the latter, it will be
Locate the connection of the nearest super class. This can be an
def retrieve_connection(klass) #:nodoc: pool = retrieve_connection_pool(klass) (pool && pool.connection) or raise ConnectionNotEstablished end
def retrieve_connection_pool(klass)
def retrieve_connection_pool(klass) pool = @connection_pools[klass.name] return pool if pool return nil if ActiveRecord::Base == klass retrieve_connection_pool klass.superclass end
def verify_active_connections! #:nodoc:
Verify active connections.
def verify_active_connections! #:nodoc: @connection_pools.each_value {|pool| pool.verify_active_connections! } end