ConnectionHandler is a collection of ConnectionPool objects. It is used for keeping separate connection pools for Active Record models that connect to different databases.
For example, suppose that you have 5 models, with the following hierarchy:
| +-- Book | | | +-- ScaryBook | +-- GoodBook +-- Author +-- BankAccount
Suppose that Book is to connect to a separate database (i.e. one other than the default database). Then Book, ScaryBook and GoodBook will all use the same connection pool. Likewise, Author and BankAccount will use the same connection pool. However, the connection pool used by Author/BankAccount is not the same as the one used by Book/ScaryBook/GoodBook.
Normally there is only a single ConnectionHandler instance, accessible via ActiveRecord::Base.connection_handler. Active Record models use this to determine that connection pool that they should use.
- A
- C
- E
- N
- R
[R] | connection_pools |
Source: show
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 368 def initialize(pools = {}) @connection_pools = pools end
Returns true if there are any active connections among the connection pools that the ConnectionHandler is managing.
Source: show
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 378 def active_connections? connection_pools.values.any? { |pool| pool.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
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 385 def clear_active_connections! @connection_pools.each_value {|pool| pool.release_connection } end
Source: show
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 394 def clear_all_connections! @connection_pools.each_value {|pool| pool.disconnect! } end
Clears the cache which maps classes.
Source: show
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 390 def clear_reloadable_connections! @connection_pools.each_value {|pool| pool.clear_reloadable_connections! } end
Returns true if a connection that’s accessible to this class has already been opened.
Source: show
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 414 def connected?(klass) conn = retrieve_connection_pool(klass) conn && conn.connected? end
Source: show
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 372 def establish_connection(name, spec) @connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec) end
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.
Source: show
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 423 def remove_connection(klass) pool = @connection_pools.delete(klass.name) return nil unless pool pool.automatic_reconnect = false pool.disconnect! pool.spec.config end
Source: show
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 432 def retrieve_connection_pool(klass) pool = @connection_pools[klass.name] return pool if pool return nil if ActiveRecord::Base == klass retrieve_connection_pool klass.superclass end