Class: ActiveRecord::ConnectionAdapters::ConnectionHandler

Inherits:
Object
  • Object
show all
Defined in:
activerecord/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 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

Instance Method Summary collapse

Constructor Details

#initialize(pools = {}) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



363
364
365
366
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 363

def initialize(pools = {})
  @connection_pools = pools
  @class_to_pool    = {}
end

Instance Attribute Details

#connection_poolsObject (readonly)

Returns the value of attribute connection_pools



361
362
363
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 361

def connection_pools
  @connection_pools
end

Instance Method Details

#active_connections?Boolean

Returns true if there are any active connections among the connection pools that the ConnectionHandler is managing.

Returns:

  • (Boolean)


375
376
377
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 375

def active_connections?
  connection_pools.values.any? { |pool| pool.active_connection? }
end

#clear_active_connections!Object

Returns any connections in use by the current thread back to the pool.



380
381
382
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 380

def clear_active_connections!
  @connection_pools.each_value {|pool| pool.release_connection }
end

#clear_all_connections!Object



389
390
391
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 389

def clear_all_connections!
  @connection_pools.each_value {|pool| pool.disconnect! }
end

#clear_reloadable_connections!Object

Clears the cache which maps classes.



385
386
387
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 385

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.

Returns:

  • (Boolean)


409
410
411
412
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 409

def connected?(klass)
  conn = retrieve_connection_pool(klass)
  conn && conn.connected?
end

#establish_connection(name, spec) ⇒ Object



368
369
370
371
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 368

def establish_connection(name, spec)
  @connection_pools[spec] ||= ConnectionAdapters::ConnectionPool.new(spec)
  @class_to_pool[name] = @connection_pools[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.



418
419
420
421
422
423
424
425
426
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 418

def remove_connection(klass)
  pool = @class_to_pool.delete(klass.name)
  return nil unless pool

  @connection_pools.delete pool.spec
  pool.automatic_reconnect = false
  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).



402
403
404
405
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 402

def retrieve_connection(klass) #:nodoc:
  pool = retrieve_connection_pool(klass)
  (pool && pool.connection) or raise ConnectionNotEstablished
end

#retrieve_connection_pool(klass) ⇒ Object



428
429
430
431
432
433
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 428

def retrieve_connection_pool(klass)
  pool = @class_to_pool[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.



394
395
396
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 394

def verify_active_connections! #:nodoc:
  @connection_pools.each_value {|pool| pool.verify_active_connections! }
end