ActiveRecord::ConnectionAdapters::ConnectionHandler (original) (raw)

Active Record Connection Handler

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
end

class BankAccount < ActiveRecord::Base
end

class Book < ActiveRecord::Base
  establish_connection :library_db
end

class ScaryBook < Book
end

class GoodBook < Book
end

And a database.yml that looked like this:

development:
  database: my_application
  host: localhost

library_db:
  database: library
  host: some.library.org

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.

Methods

A

C

E

F

N

R

Class Public methods

Source: show | on GitHub

def initialize

@connection_name_to_pool_manager = Concurrent::Map.new(initial_capacity: 2) end

Instance Public methods

Returns true if there are any active connections among the connection pools that the ConnectionHandler is managing.

Source: show | on GitHub

def active_connections?(role = nil) each_connection_pool(role).any?(&:active_connection?) end

Returns any connections in use by the current thread back to the pool, and also returns connections to the pool cached by threads that are no longer alive.

Source: show | on GitHub

def clear_active_connections!(role = nil) each_connection_pool(role).each do |pool| pool.release_connection pool.disable_query_cache! end end

Source: show | on GitHub

def clear_all_connections!(role = nil) each_connection_pool(role).each(&:disconnect!) end

Source: show | on GitHub

def clear_reloadable_connections!(role = nil) each_connection_pool(role).each(&:clear_reloadable_connections!) end

Returns true if a connection that’s accessible to this class has already been opened.

Source: show | on GitHub

def connected?(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) pool = retrieve_connection_pool(connection_name, role: role, shard: shard) pool && pool.connected? end

Returns the pools for a connection handler and given role. If :all is passed, all pools belonging to the connection handler will be returned.

Source: show | on GitHub

def connection_pool_list(role = nil) if role.nil? || role == :all connection_name_to_pool_manager.values.flat_map { |m| m.pool_configs.map(&:pool) } else connection_name_to_pool_manager.values.flat_map { |m| m.pool_configs(role).map(&:pool) } end end

Source: show | on GitHub

def establish_connection(config, owner_name: Base, role: Base.current_role, shard: Base.current_shard, clobber: false) owner_name = determine_owner_name(owner_name, config)

pool_config = resolve_pool_config(config, owner_name, role, shard) db_config = pool_config.db_config

pool_manager = set_pool_manager(pool_config.connection_descriptor)

existing_pool_config = pool_manager.get_pool_config(role, shard)

if !clobber && existing_pool_config && existing_pool_config.db_config == db_config

if owner_name.primary_class? && (existing_pool_config.connection_descriptor != owner_name)
  existing_pool_config.connection_descriptor = owner_name
end

existing_pool_config.pool

else disconnect_pool_from_pool_manager(pool_manager, role, shard) pool_manager.set_pool_config(role, shard, pool_config)

payload = {
  connection_name: pool_config.connection_descriptor.name,
  role: role,
  shard: shard,
  config: db_config.configuration_hash
}

ActiveSupport::Notifications.instrumenter.instrument("!connection.active_record", payload) do
  pool_config.pool
end

end end

Source: show | on GitHub

def flush_idle_connections!(role = nil) each_connection_pool(role).each(&:flush!) end

Source: show | on GitHub

def remove_connection_pool(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) if pool_manager = get_pool_manager(connection_name) disconnect_pool_from_pool_manager(pool_manager, role, shard) end end

Retrieving the connection pool happens a lot, so we cache it in @connection_name_to_pool_manager. This makes retrieving the connection pool O(1) once the process is warm. When a connection is established or removed, we invalidate the cache.

Source: show | on GitHub

def retrieve_connection_pool(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard, strict: false) pool_manager = get_pool_manager(connection_name) pool = pool_manager&.get_pool_config(role, shard)&.pool

if strict && !pool selector = [ ("'#{shard}' shard" unless shard == ActiveRecord::Base.default_shard), ("'#{role}' role" unless role == ActiveRecord::Base.default_role), ].compact.join(" and ")

selector = [
  (connection_name unless connection_name == "ActiveRecord::Base"),
  selector.presence,
].compact.join(" with ")

selector = " for #{selector}" if selector.present?

message = "No database connection defined#{selector}."

raise ConnectionNotDefined.new(message, connection_name: connection_name, shard: shard, role: role)

end

pool end