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.
# File activerecord/lib/active_record/connection_adapters/abstract/connection_handler.rb, line 211
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