Class: ActiveRecord::ConnectionAdapters::ConnectionHandler
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::ConnectionHandler
- Defined in:
- lib/active_record/connection_adapters/abstract/connection_handler.rb
Overview
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.
Defined Under Namespace
Classes: StringConnectionName
Instance Method Summary collapse
-
#active_connections?(role = nil) ⇒ Boolean
Returns true if there are any active connections among the connection pools that the ConnectionHandler is managing.
-
#clear_active_connections!(role = nil) ⇒ Object
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.
- #clear_all_connections!(role = nil) ⇒ Object
-
#clear_reloadable_connections!(role = nil) ⇒ Object
Clears the cache which maps classes.
-
#connected?(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) ⇒ Boolean
Returns true if a connection that’s accessible to this class has already been opened.
-
#connection_pool_list(role = nil) ⇒ Object
(also: #connection_pools)
Returns the pools for a connection handler and given role.
-
#connection_pool_names ⇒ Object
:nodoc:.
-
#each_connection_pool(role = nil, &block) ⇒ Object
:nodoc:.
- #establish_connection(config, owner_name: Base, role: Base.current_role, shard: Base.current_shard, clobber: false) ⇒ Object
-
#flush_idle_connections!(role = nil) ⇒ Object
Disconnects all currently idle connections.
-
#initialize ⇒ ConnectionHandler
constructor
A new instance of ConnectionHandler.
-
#prevent_writes ⇒ Object
:nodoc:.
-
#prevent_writes=(prevent_writes) ⇒ Object
:nodoc:.
- #remove_connection_pool(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) ⇒ Object
-
#retrieve_connection(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) ⇒ Object
Locate the connection of the nearest super class.
-
#retrieve_connection_pool(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard, strict: false) ⇒ Object
Retrieving the connection pool happens a lot, so we cache it in @connection_name_to_pool_manager.
Constructor Details
#initialize ⇒ ConnectionHandler
Returns a new instance of ConnectionHandler.
73 74 75 76 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 73 def initialize # These caches are keyed by pool_config.connection_name (PoolConfig#connection_name). @connection_name_to_pool_manager = Concurrent::Map.new(initial_capacity: 2) end |
Instance Method Details
#active_connections?(role = nil) ⇒ Boolean
Returns true if there are any active connections among the connection pools that the ConnectionHandler is managing.
154 155 156 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 154 def active_connections?(role = nil) each_connection_pool(role).any?(&:active_connection?) end |
#clear_active_connections!(role = nil) ⇒ Object
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.
161 162 163 164 165 166 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 161 def clear_active_connections!(role = nil) each_connection_pool(role).each do |pool| pool.release_connection pool.disable_query_cache! end end |
#clear_all_connections!(role = nil) ⇒ Object
175 176 177 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 175 def clear_all_connections!(role = nil) each_connection_pool(role).each(&:disconnect!) end |
#clear_reloadable_connections!(role = nil) ⇒ Object
Clears the cache which maps classes.
See ConnectionPool#clear_reloadable_connections! for details.
171 172 173 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 171 def clear_reloadable_connections!(role = nil) each_connection_pool(role).each(&:clear_reloadable_connections!) end |
#connected?(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) ⇒ Boolean
Returns true if a connection that’s accessible to this class has already been opened.
197 198 199 200 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 197 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 |
#connection_pool_list(role = nil) ⇒ Object Also known as: connection_pools
Returns the pools for a connection handler and given role. If :all
is passed, all pools belonging to the connection handler will be returned.
92 93 94 95 96 97 98 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 92 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 |
#connection_pool_names ⇒ Object
:nodoc:
86 87 88 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 86 def connection_pool_names # :nodoc: connection_name_to_pool_manager.keys end |
#each_connection_pool(role = nil, &block) ⇒ Object
:nodoc:
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 101 def each_connection_pool(role = nil, &block) # :nodoc: role = nil if role == :all return enum_for(__method__, role) unless block_given? connection_name_to_pool_manager.each_value do |manager| manager.each_pool_config(role) do |pool_config| yield pool_config.pool end end end |
#establish_connection(config, owner_name: Base, role: Base.current_role, shard: Base.current_shard, clobber: false) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 112 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_name) # If there is an existing pool with the same values as the pool_config # don't remove the connection. Connections should only be removed if we are # establishing a connection on a class that is already connected to a different # configuration. existing_pool_config = pool_manager.get_pool_config(role, shard) if !clobber && existing_pool_config && existing_pool_config.db_config == db_config # Update the pool_config's connection class if it differs. This is used # for ensuring that ActiveRecord::Base and the primary_abstract_class use # the same pool. Without this granular swapping will not work correctly. if owner_name.primary_class? && (existing_pool_config.connection_class != owner_name) existing_pool_config.connection_class = 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_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 |
#flush_idle_connections!(role = nil) ⇒ Object
Disconnects all currently idle connections.
See ConnectionPool#flush! for details.
182 183 184 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 182 def flush_idle_connections!(role = nil) each_connection_pool(role).each(&:flush!) end |
#prevent_writes ⇒ Object
:nodoc:
78 79 80 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 78 def prevent_writes # :nodoc: ActiveSupport::IsolatedExecutionState[:active_record_prevent_writes] end |
#prevent_writes=(prevent_writes) ⇒ Object
:nodoc:
82 83 84 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 82 def prevent_writes=(prevent_writes) # :nodoc: ActiveSupport::IsolatedExecutionState[:active_record_prevent_writes] = prevent_writes end |
#remove_connection_pool(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) ⇒ Object
202 203 204 205 206 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 202 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 |
#retrieve_connection(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) ⇒ Object
Locate the connection of the nearest super class. This can be an active or defined connection: if it is the latter, it will be opened and set as the active connection for the class it was defined for (not necessarily the current class).
190 191 192 193 |
# File 'lib/active_record/connection_adapters/abstract/connection_handler.rb', line 190 def retrieve_connection(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) # :nodoc: pool = retrieve_connection_pool(connection_name, role: role, shard: shard, strict: true) pool.lease_connection end |
#retrieve_connection_pool(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard, strict: false) ⇒ Object
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.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File '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? = "No database connection defined#{selector}." raise ConnectionNotDefined.new(, connection_name: connection_name, shard: shard, role: role) end pool end |