Connection pool base class for managing ActiveRecord database connections.
Introduction
A connection pool synchronizes thread access to a limited number of database connections. The basic idea is that each thread checks out a database connection from the pool, uses that connection, and checks the connection back in. ConnectionPool is completely thread-safe, and will ensure that a connection cannot be used by two threads at the same time, as long as ConnectionPool‘s contract is correctly followed. It will also handle cases in which there are more threads than connections: if all connections have been checked out, and a thread tries to checkout a connection anyway, then ConnectionPool will wait until some other thread has checked in a connection.
Obtaining (checking out) a connection
Connections can be obtained and used from a connection pool in several ways:
- Simply use ActiveRecord::Base.connection as with ActiveRecord 2.1 and earlier (pre-connection-pooling). Eventually, when you‘re done with the connection(s) and wish it to be returned to the pool, you call ActiveRecord::Base.clear_active_connections!. This will be the default behavior for ActiveRecord when used in conjunction with ActionPack‘s request handling cycle.
- Manually check out a connection from the pool with ActiveRecord::Base.connection_pool.checkout. You are responsible for returning this connection to the pool when finished by calling ActiveRecord::Base.connection_pool.checkin(connection).
- Use ActiveRecord::Base.connection_pool.with_connection(&block), which obtains a connection, yields it as the sole argument to the block, and returns it to the pool after the block completes.
Connections in the pool are actually AbstractAdapter objects (or objects compatible with AbstractAdapter‘s interface).
Options
There are two connection-pooling-related options that you can add to your database connection configuration:
- pool: number indicating size of connection pool (default 5)
- wait_timeout: number of seconds to block and wait for a connection before giving up and raising a timeout error (default 5 seconds).
- checkin
- checkout
- clear_reloadable_connections!
- clear_stale_cached_connections!
- connected?
- connection
- disconnect!
- new
- release_connection
- with_connection
[R] | spec |
Creates a new ConnectionPool object. spec is a ConnectionSpecification object which describes database connection information (e.g. adapter, host name, username, password, etc), as well as the maximum size for this ConnectionPool.
The default ConnectionPool maximum size is 5.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 66 66: def initialize(spec) 67: @spec = spec 68: 69: # The cache of reserved connections mapped to threads 70: @reserved_connections = {} 71: 72: # The mutex used to synchronize pool access 73: @connection_mutex = Monitor.new 74: @queue = @connection_mutex.new_cond 75: 76: # default 5 second timeout unless on ruby 1.9 77: @timeout = 78: if RUBY_VERSION < '1.9' 79: spec.config[:wait_timeout] || 5 80: end 81: 82: # default max pool size to 5 83: @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5 84: 85: @connections = [] 86: @checked_out = [] 87: end
Check-in a database connection back into the pool, indicating that you no longer need this connection.
conn: an AbstractAdapter object, which was obtained by earlier by calling checkout on this pool.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 210 210: def checkin(conn) 211: @connection_mutex.synchronize do 212: conn.run_callbacks :checkin 213: @checked_out.delete conn 214: @queue.signal 215: end 216: end
Check-out a database connection from the pool, indicating that you want to use it. You should call checkin when you no longer need this.
This is done by either returning an existing connection, or by creating a new connection. If the maximum number of connections for this pool has already been reached, but the pool is empty (i.e. they‘re all being used), then this method will wait until a thread has checked in a connection. The wait time is bounded however: if no connection can be checked out within the timeout specified for this pool, then a ConnectionTimeoutError exception will be raised.
Returns: an AbstractAdapter object.
Raises:
- ConnectionTimeoutError: no connection can be obtained from the pool within the timeout period.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 181 181: def checkout 182: # Checkout an available connection 183: @connection_mutex.synchronize do 184: loop do 185: conn = if @checked_out.size < @connections.size 186: checkout_existing_connection 187: elsif @connections.size < @size 188: checkout_new_connection 189: end 190: return conn if conn 191: # No connections available; wait for one 192: if @queue.wait(@timeout) 193: next 194: else 195: # try looting dead threads 196: clear_stale_cached_connections! 197: if @size == @checked_out.size 198: raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it." 199: end 200: end 201: end 202: end 203: end
Clears the cache which maps classes
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 137 137: def clear_reloadable_connections! 138: @reserved_connections.each do |name, conn| 139: checkin conn 140: end 141: @reserved_connections = {} 142: @connections.each do |conn| 143: conn.disconnect! if conn.requires_reloading? 144: end 145: @connections = [] 146: end
Return any checked-out connections back to the pool by threads that are no longer alive.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 159 159: def clear_stale_cached_connections! 160: remove_stale_cached_threads!(@reserved_connections) do |name, conn| 161: checkin conn 162: end 163: end
Returns true if a connection has already been opened.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 120 120: def connected? 121: !@connections.empty? 122: end
Retrieve the connection associated with the current thread, or call checkout to obtain one if necessary.
connection can be called any number of times; the connection is held in a hash keyed by the thread id.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 94 94: def connection 95: if conn = @reserved_connections[current_connection_id] 96: conn 97: else 98: @reserved_connections[current_connection_id] = checkout 99: end 100: end
Disconnects all connections in the pool, and clears the pool.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 125 125: def disconnect! 126: @reserved_connections.each do |name,conn| 127: checkin conn 128: end 129: @reserved_connections = {} 130: @connections.each do |conn| 131: conn.disconnect! 132: end 133: @connections = [] 134: end
Signal that the thread is finished with the current connection. release_connection releases the connection-thread association and returns the connection to the pool.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 105 105: def release_connection 106: conn = @reserved_connections.delete(current_connection_id) 107: checkin conn if conn 108: end
Reserve a connection, and yield it to a block. Ensure the connection is checked back in when finished.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 112 112: def with_connection 113: conn = checkout 114: yield conn 115: ensure 116: checkin conn 117: end