Class: ActiveRecord::ConnectionAdapters::ConnectionHandler
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::ConnectionHandler
- Defined in:
- lib/activerecord_threadsafe_fix_3_0.rb
Overview
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.
Instance Attribute Summary collapse
-
#connection_pools ⇒ Object
readonly
Returns the value of attribute connection_pools.
Instance Method Summary collapse
-
#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?(klass) ⇒ Boolean
Returns true if a connection that’s accessible to this class has already been opened.
- #establish_connection(name, spec) ⇒ Object
-
#initialize(pools = {}) ⇒ ConnectionHandler
constructor
A new instance of ConnectionHandler.
-
#remove_connection(klass) ⇒ Object
Remove the connection for this class.
-
#retrieve_connection(klass) ⇒ Object
Locate the connection of the nearest super class.
- #retrieve_connection_pool(klass) ⇒ Object
-
#verify_active_connections! ⇒ Object
Verify active connections.
Constructor Details
#initialize(pools = {}) ⇒ ConnectionHandler
Returns a new instance of ConnectionHandler.
289 290 291 |
# File 'lib/activerecord_threadsafe_fix_3_0.rb', line 289 def initialize(pools = {}) @connection_pools = pools end |
Instance Attribute Details
#connection_pools ⇒ Object (readonly)
Returns the value of attribute connection_pools.
287 288 289 |
# File 'lib/activerecord_threadsafe_fix_3_0.rb', line 287 def connection_pools @connection_pools end |
Instance Method Details
#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.
300 301 302 |
# File 'lib/activerecord_threadsafe_fix_3_0.rb', line 300 def clear_active_connections! @connection_pools.each_value {|pool| pool.release_connection } end |
#clear_all_connections! ⇒ Object
309 310 311 |
# File 'lib/activerecord_threadsafe_fix_3_0.rb', line 309 def clear_all_connections! @connection_pools.each_value {|pool| pool.disconnect! } end |
#clear_reloadable_connections! ⇒ Object
Clears the cache which maps classes
305 306 307 |
# File 'lib/activerecord_threadsafe_fix_3_0.rb', line 305 def clear_reloadable_connections! @connection_pools.each_value {|pool| pool.clear_reloadable_connections! } end |
#connected?(klass) ⇒ Boolean
Returns true if a connection that’s accessible to this class has already been opened.
329 330 331 332 |
# File 'lib/activerecord_threadsafe_fix_3_0.rb', line 329 def connected?(klass) conn = retrieve_connection_pool(klass) conn && conn.connected? end |
#establish_connection(name, spec) ⇒ Object
293 294 295 |
# File 'lib/activerecord_threadsafe_fix_3_0.rb', line 293 def establish_connection(name, spec) @connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec) end |
#remove_connection(klass) ⇒ 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.
338 339 340 341 342 343 344 345 |
# File 'lib/activerecord_threadsafe_fix_3_0.rb', line 338 def remove_connection(klass) pool = @connection_pools[klass.name] return nil unless pool @connection_pools.delete_if { |key, value| value == pool } pool.disconnect! pool.spec.config end |
#retrieve_connection(klass) ⇒ 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).
322 323 324 325 |
# File 'lib/activerecord_threadsafe_fix_3_0.rb', line 322 def retrieve_connection(klass) #:nodoc: pool = retrieve_connection_pool(klass) (pool && pool.connection) or raise ConnectionNotEstablished end |
#retrieve_connection_pool(klass) ⇒ Object
347 348 349 350 351 352 |
# File 'lib/activerecord_threadsafe_fix_3_0.rb', line 347 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 |
#verify_active_connections! ⇒ Object
Verify active connections.
314 315 316 |
# File 'lib/activerecord_threadsafe_fix_3_0.rb', line 314 def verify_active_connections! #:nodoc: @connection_pools.each_value {|pool| pool.verify_active_connections! } end |