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 the connection pool that they should use.

Instance Method Summary collapse

Constructor Details

#initializeConnectionHandler

Returns a new instance of ConnectionHandler


483
484
485
486
487
488
489
490
491
492
493
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 483

def initialize
  # These caches are keyed by klass.name, NOT klass. Keying them by klass
  # alone would lead to memory leaks in development mode as all previous
  # instances of the class would stay in memory.
  @owner_to_pool = ThreadSafe::Cache.new(:initial_capacity => 2) do |h,k|
    h[k] = ThreadSafe::Cache.new(:initial_capacity => 2)
  end
  @class_to_pool = ThreadSafe::Cache.new(:initial_capacity => 2) do |h,k|
    h[k] = ThreadSafe::Cache.new
  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.

Returns:

  • (Boolean)

515
516
517
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 515

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.


522
523
524
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 522

def clear_active_connections!
  connection_pool_list.each(&:release_connection)
end

#clear_all_connections!Object


531
532
533
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 531

def clear_all_connections!
  connection_pool_list.each(&:disconnect!)
end

#clear_reloadable_connections!Object

Clears the cache which maps classes.


527
528
529
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 527

def clear_reloadable_connections!
  connection_pool_list.each(&:clear_reloadable_connections!)
end

#connected?(klass) ⇒ Boolean

Returns true if a connection that's accessible to this class has already been opened.

Returns:

  • (Boolean)

546
547
548
549
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 546

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

#connection_pool_listObject


495
496
497
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 495

def connection_pool_list
  owner_to_pool.values.compact
end

#connection_poolsObject


499
500
501
502
503
504
505
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 499

def connection_pools
  ActiveSupport::Deprecation.warn(
    "In the next release, this will return the same as #connection_pool_list. " \
    "(An array of pools, rather than a hash mapping specs to pools.)"
  )
  Hash[connection_pool_list.map { |pool| [pool.spec, pool] }]
end

#establish_connection(owner, spec) ⇒ Object

Raises:

  • (RuntimeError)

507
508
509
510
511
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 507

def establish_connection(owner, spec)
  @class_to_pool.clear
  raise RuntimeError, "Anonymous class is not allowed." unless owner.name
  owner_to_pool[owner.name] = ConnectionAdapters::ConnectionPool.new(spec)
end

#remove_connection(owner) ⇒ 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.


555
556
557
558
559
560
561
562
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 555

def remove_connection(owner)
  if pool = owner_to_pool.delete(owner.name)
    @class_to_pool.clear
    pool.automatic_reconnect = false
    pool.disconnect!
    pool.spec.config
  end
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).


539
540
541
542
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 539

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

#retrieve_connection_pool(klass) ⇒ Object

Retrieving the connection pool happens a lot so we cache it in @class_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.

Ideally we would use #fetch here, as class_to_pool may sometimes be nil. However, benchmarking (gist.github.com/jonleighton/3552829) showed that #fetch is significantly slower than #[]. So in the nil case, no caching will take place, but that's ok since the nil case is not the common one that we wish to optimise for.


573
574
575
576
577
578
579
580
581
582
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb', line 573

def retrieve_connection_pool(klass)
  class_to_pool[klass.name] ||= begin
    until pool = pool_for(klass)
      klass = klass.superclass
      break unless klass <= Base
    end

    class_to_pool[klass.name] = pool
  end
end