Class: ActiveRecord::ConnectionAdapters::ConnectionHandler
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::ConnectionHandler
- Defined in:
- 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 that connect to different databases.
For example, suppose that you have 5 models, with the following hierarchy:
class Author < ActiveRecord::Base
end
class BankAccount < ActiveRecord::Base
end
class Book < ActiveRecord::Base
establish_connection "library_db"
end
class ScaryBook < Book
end
class GoodBook < Book
end
And a database.yml that looked like this:
development:
database: my_application
host: localhost
library_db:
database: library
host: some.library.org
Your primary database in the development environment is “my_application” but the Book model connects to a separate database called “library_db” (this can even be a database on a different machine).
Book, ScaryBook and GoodBook will all use the same connection pool to “library_db” while Author, BankAccount, and any other models you create will use the default connection pool to “my_application”.
The various connection pools are managed by a single instance of ConnectionHandler accessible via ActiveRecord::Base.connection_handler. All Active Record models use this handler to determine the connection pool that they should use.
The ConnectionHandler class is not coupled with the Active models, as it has no knowlodge about the model. The model, needs to pass a specification name to the handler, in order to lookup the correct connection pool.
Instance Method Summary collapse
-
#active_connections? ⇒ Boolean
Returns true if there are any active connections among the connection pools that the ConnectionHandler is managing.
-
#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?(spec_name) ⇒ Boolean
Returns true if a connection that’s accessible to this class has already been opened.
- #connection_pool_list ⇒ Object (also: #connection_pools)
- #establish_connection(spec) ⇒ Object
-
#initialize ⇒ ConnectionHandler
constructor
A new instance of ConnectionHandler.
-
#remove_connection(spec_name) ⇒ Object
Remove the connection for this class.
-
#retrieve_connection(spec_name) ⇒ Object
Locate the connection of the nearest super class.
-
#retrieve_connection_pool(spec_name) ⇒ Object
Retrieving the connection pool happens a lot so we cache it in @class_to_pool.
Constructor Details
#initialize ⇒ ConnectionHandler
Returns a new instance of ConnectionHandler.
828 829 830 831 832 833 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 828 def initialize # These caches are keyed by spec.name (ConnectionSpecification#name). @owner_to_pool = Concurrent::Map.new(:initial_capacity => 2) do |h,k| h[k] = Concurrent::Map.new(:initial_capacity => 2) 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.
846 847 848 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 846 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.
853 854 855 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 853 def clear_active_connections! connection_pool_list.each(&:release_connection) end |
#clear_all_connections! ⇒ Object
864 865 866 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 864 def clear_all_connections! connection_pool_list.each(&:disconnect!) end |
#clear_reloadable_connections! ⇒ Object
Clears the cache which maps classes.
See ConnectionPool#clear_reloadable_connections! for details.
860 861 862 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 860 def clear_reloadable_connections! connection_pool_list.each(&:clear_reloadable_connections!) end |
#connected?(spec_name) ⇒ Boolean
Returns true if a connection that’s accessible to this class has already been opened.
882 883 884 885 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 882 def connected?(spec_name) conn = retrieve_connection_pool(spec_name) conn && conn.connected? end |
#connection_pool_list ⇒ Object Also known as: connection_pools
835 836 837 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 835 def connection_pool_list owner_to_pool.values.compact end |
#establish_connection(spec) ⇒ Object
840 841 842 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 840 def establish_connection(spec) owner_to_pool[spec.name] = ConnectionAdapters::ConnectionPool.new(spec) end |
#remove_connection(spec_name) ⇒ 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.
891 892 893 894 895 896 897 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 891 def remove_connection(spec_name) if pool = owner_to_pool.delete(spec_name) pool.automatic_reconnect = false pool.disconnect! pool.spec.config end end |
#retrieve_connection(spec_name) ⇒ 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).
872 873 874 875 876 877 878 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 872 def retrieve_connection(spec_name) #:nodoc: pool = retrieve_connection_pool(spec_name) raise ConnectionNotEstablished, "No connection pool with id #{spec_name} found." unless pool conn = pool.connection raise ConnectionNotEstablished, "No connection for #{spec_name} in connection pool" unless conn conn end |
#retrieve_connection_pool(spec_name) ⇒ 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.
908 909 910 911 912 913 914 915 916 917 918 919 920 921 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 908 def retrieve_connection_pool(spec_name) owner_to_pool.fetch(spec_name) do if ancestor_pool = pool_from_any_process_for(spec_name) # A connection was established in an ancestor process that must have # subsequently forked. We can't reuse the connection, but we can copy # the specification and establish a new connection with it. establish_connection(ancestor_pool.spec).tap do |pool| pool.schema_cache = ancestor_pool.schema_cache if ancestor_pool.schema_cache end else owner_to_pool[spec_name] = nil end end end |