ConnectionHandler is a collection of ConnectionPool objects. It is used for keeping separate connection pools for ActiveRecord 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. ActiveRecord models use this to determine that connection pool that they should use.
- clear_active_connections!
- clear_all_connections!
- clear_reloadable_connections!
- connected?
- connection_pools
- establish_connection
- new
- remove_connection
- retrieve_connection_pool
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 287 287: def initialize(pools = {}) 288: @connection_pools = pools 289: 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.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 302 302: def clear_active_connections! 303: @connection_pools.each_value {|pool| pool.release_connection } 304: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 311 311: def clear_all_connections! 312: @connection_pools.each_value {|pool| pool.disconnect! } 313: end
Clears the cache which maps classes
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 307 307: def clear_reloadable_connections! 308: @connection_pools.each_value {|pool| pool.clear_reloadable_connections! } 309: end
Returns true if a connection that‘s accessible to this class has already been opened.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 331 331: def connected?(klass) 332: conn = retrieve_connection_pool(klass) 333: conn ? conn.connected? : false 334: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 291 291: def connection_pools 292: @connection_pools ||= {} 293: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 295 295: def establish_connection(name, spec) 296: @connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec) 297: 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.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 340 340: def remove_connection(klass) 341: pool = @connection_pools[klass.name] 342: @connection_pools.delete_if { |key, value| value == pool } 343: pool.disconnect! if pool 344: pool.spec.config if pool 345: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 347 347: def retrieve_connection_pool(klass) 348: pool = @connection_pools[klass.name] 349: return pool if pool 350: return nil if ActiveRecord::Base == klass 351: retrieve_connection_pool klass.superclass 352: end