Class: ActiveRecord::ConnectionAdapters::ConnectionHandler
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::ConnectionHandler
- Defined in:
- lib/active_record/connection_adapters/abstract/connection_pool.rb
Overview
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 specification name to the handler, in order to look up the correct connection pool.
Class Method Summary collapse
-
.create_owner_to_pool ⇒ Object
:nodoc:.
-
.discard_unowned_pools(pid_map) ⇒ Object
:nodoc:.
-
.unowned_pool_finalizer(pid_map) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#active_connections? ⇒ Boolean
Returns true if there are any active connections among the connection pools that the ConnectionHandler is managing.
-
#clear_active_connections! ⇒ 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! ⇒ Object
-
#clear_reloadable_connections! ⇒ Object
Clears the cache which maps classes.
-
#connected?(spec_name) ⇒ Boolean
Returns true if a connection that’s accessible to this class has already been opened.
- #connection_pool_list ⇒ Object (also: #connection_pools)
- #establish_connection(config) ⇒ Object
-
#flush_idle_connections! ⇒ 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(spec_name) ⇒ Object
Remove the connection for this class.
-
#retrieve_connection(spec_name) ⇒ Object
Locate the connection of the nearest super class.
-
#retrieve_connection_pool(spec_name) ⇒ Object
Retrieving the connection pool happens a lot, so we cache it in @owner_to_pool.
-
#while_preventing_writes(enabled = true) ⇒ Object
Prevent writing to the database regardless of role.
Constructor Details
#initialize ⇒ ConnectionHandler
Returns a new instance of ConnectionHandler.
1016 1017 1018 1019 1020 1021 1022 1023 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1016 def initialize # These caches are keyed by spec.name (ConnectionSpecification#name). @owner_to_pool = ConnectionHandler.create_owner_to_pool # Backup finalizer: if the forked child never needed a pool, the above # early discard has not occurred ObjectSpace.define_finalizer self, ConnectionHandler.unowned_pool_finalizer(@owner_to_pool) end |
Class Method Details
.create_owner_to_pool ⇒ Object
:nodoc:
994 995 996 997 998 999 1000 1001 1002 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 994 def self.create_owner_to_pool # :nodoc: Concurrent::Map.new(initial_capacity: 2) do |h, k| # Discard the parent's connection pools immediately; we have no need # of them discard_unowned_pools(h) h[k] = Concurrent::Map.new(initial_capacity: 2) end end |
.discard_unowned_pools(pid_map) ⇒ Object
:nodoc:
1010 1011 1012 1013 1014 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1010 def self.discard_unowned_pools(pid_map) # :nodoc: pid_map.each do |pid, pools| pools.values.compact.each(&:discard!) unless pid == Process.pid end end |
.unowned_pool_finalizer(pid_map) ⇒ Object
:nodoc:
1004 1005 1006 1007 1008 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1004 def self.unowned_pool_finalizer(pid_map) # :nodoc: lambda do |_| discard_unowned_pools(pid_map) end end |
Instance Method Details
#active_connections? ⇒ Boolean
Returns true if there are any active connections among the connection pools that the ConnectionHandler is managing.
1080 1081 1082 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1080 def active_connections? connection_pool_list.any?(&:active_connection?) end |
#clear_active_connections! ⇒ 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.
1087 1088 1089 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1087 def clear_active_connections! connection_pool_list.each(&:release_connection) end |
#clear_all_connections! ⇒ Object
1098 1099 1100 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1098 def clear_all_connections! connection_pool_list.each(&:disconnect!) end |
#clear_reloadable_connections! ⇒ Object
Clears the cache which maps classes.
See ConnectionPool#clear_reloadable_connections! for details.
1094 1095 1096 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1094 def clear_reloadable_connections! connection_pool_list.each(&:clear_reloadable_connections!) end |
#connected?(spec_name) ⇒ Boolean
Returns true if a connection that’s accessible to this class has already been opened.
1130 1131 1132 1133 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1130 def connected?(spec_name) pool = retrieve_connection_pool(spec_name) pool && pool.connected? end |
#connection_pool_list ⇒ Object Also known as: connection_pools
1051 1052 1053 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1051 def connection_pool_list owner_to_pool.values.compact end |
#establish_connection(config) ⇒ Object
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1056 def establish_connection(config) resolver = ConnectionSpecification::Resolver.new(Base.configurations) spec = resolver.spec(config) remove_connection(spec.name) = ActiveSupport::Notifications.instrumenter payload = { connection_id: object_id } if spec payload[:spec_name] = spec.name payload[:config] = spec.config end .instrument("!connection.active_record", payload) do owner_to_pool[spec.name] = ConnectionAdapters::ConnectionPool.new(spec) end owner_to_pool[spec.name] end |
#flush_idle_connections! ⇒ Object
Disconnects all currently idle connections.
See ConnectionPool#flush! for details.
1105 1106 1107 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1105 def flush_idle_connections! connection_pool_list.each(&:flush!) end |
#prevent_writes ⇒ Object
:nodoc:
1025 1026 1027 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1025 def prevent_writes # :nodoc: Thread.current[:prevent_writes] end |
#prevent_writes=(prevent_writes) ⇒ Object
:nodoc:
1029 1030 1031 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1029 def prevent_writes=(prevent_writes) # :nodoc: Thread.current[:prevent_writes] = prevent_writes end |
#remove_connection(spec_name) ⇒ Object
Remove the connection for this class. This will close the active connection and the defined connection (if they exist). The result can be used as an argument for #establish_connection, for easily re-establishing the connection.
1139 1140 1141 1142 1143 1144 1145 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1139 def remove_connection(spec_name) if pool = owner_to_pool.delete(spec_name) pool.automatic_reconnect = false pool.disconnect! pool.spec.config end end |
#retrieve_connection(spec_name) ⇒ 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).
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1113 def retrieve_connection(spec_name) #:nodoc: pool = retrieve_connection_pool(spec_name) unless pool # multiple database application if ActiveRecord::Base.connection_handler != ActiveRecord::Base.default_connection_handler raise ConnectionNotEstablished, "No connection pool with '#{spec_name}' found for the '#{ActiveRecord::Base.current_role}' role." else raise ConnectionNotEstablished, "No connection pool with '#{spec_name}' found." end end pool.connection end |
#retrieve_connection_pool(spec_name) ⇒ Object
Retrieving the connection pool happens a lot, so we cache it in @owner_to_pool. This makes retrieving the connection pool O(1) once the process is warm. When a connection is established or removed, we invalidate the cache.
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1150 def retrieve_connection_pool(spec_name) owner_to_pool.fetch(spec_name) do # Check if a connection was previously established in an ancestor process, # which may have been forked. if ancestor_pool = pool_from_any_process_for(spec_name) # A connection was established in an ancestor process that must have # subsequently forked. We can't reuse the connection, but we can copy # the specification and establish a new connection with it. establish_connection(ancestor_pool.spec.to_hash).tap do |pool| pool.schema_cache = ancestor_pool.schema_cache if ancestor_pool.schema_cache end else owner_to_pool[spec_name] = nil end end end |
#while_preventing_writes(enabled = true) ⇒ Object
Prevent writing to the database regardless of role.
In some cases you may want to prevent writes to the database even if you are on a database that can write. ‘while_preventing_writes` will prevent writes to the database for the duration of the block.
This method does not provide the same protection as a readonly user and is meant to be a safeguard against accidental writes.
See ‘READ_QUERY` for the queries that are blocked by this method.
1044 1045 1046 1047 1048 1049 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 1044 def while_preventing_writes(enabled = true) original, self.prevent_writes = self.prevent_writes, enabled yield ensure self.prevent_writes = original end |