ConnectionHandler is a collection of ConnectionPool objects. It is used for keeping separate connection pools that connect to different databases.

For example, suppose that you have 5 models, with the following hierarchy:

class Author < ActiveRecord::Base

class BankAccount < ActiveRecord::Base

class Book < ActiveRecord::Base
  establish_connection :library_db

class ScaryBook < Book

class GoodBook < Book

And a database.yml that looked like this:

  database: my_application
  host: localhost

  database: library

Your primary database in the development environment is “my_application” but the Book model connects to a separate database called “library_db” (this can even be a database on a different machine).

Book, ScaryBook and GoodBook will all use the same connection pool to “library_db” while Author, BankAccount, and any other models you create will use the default connection pool to “my_application”.

The various connection pools are managed by a single instance of ConnectionHandler accessible via ActiveRecord::Base.connection_handler. All Active Record models use this handler to determine the connection pool that they should use.

The ConnectionHandler class is not coupled with the Active models, as it has no knowledge about the model. The model needs to pass a connection specification name to the handler, in order to look up the correct connection pool.


FINALIZER = lambda { |_| ActiveSupport::ForkTracker.check! }


[R] owner_to_pool_manager
Show files where this class is defined (1 file)
Register or log in to add new notes.