Voting

ActiveRecord::Base#connection_pool

📁 activerecord/lib/active_record/connection_adapters/pool.rb
14 / 20 votes 70% to consensus
📄 Source Code Show context
class ActiveRecord::Base
142 # Returns the connection pool associated with this class.
143 # Raises +ConnectionNotEstablished+ if no pool is found.
144 def connection_pool
145 pool_manager.pool_config(pool_key)&.pool || raise(ConnectionNotEstablished)
146 end
147
148 # Returns the pool manager for the current handler.
149 def pool_manager
150 @pool_manager ||= PoolManager.new
151 end
Version A 8 votes

connection_pool

Returns the ConnectionPool instance associated with the current class and connection specification. The pool manages a finite set of reusable database connections, providing thread-safe checkout and checkin semantics.

Parameters
pool_key (Symbol) — The key identifying which pool configuration to use. Defaults to the current handler's writing role.
Returns

ConnectionPool — The active connection pool for this class.

Raises

ConnectionNotEstablished — If no pool configuration exists for the given pool key, or if establish_connection has not been called.

Version B 4 votes

connection_pool

Think of the connection pool as a checkout counter at a library. Your application has a limited number of database connections available, and each thread needs to "check out" one before it can talk to the database.

This method gives you access to that checkout counter. It looks up the right pool for your model's configuration and hands it back. If no pool has been set up yet (i.e., you haven't called establish_connection), it raises an error to let you know.

Most of the time you won't call this directly — ActiveRecord handles it behind the scenes. But if you're debugging connection issues or building custom connection logic, this is your entry point.

Version C 2 votes

connection_pool

Returns the ConnectionPool for this model. Raises ConnectionNotEstablished if unavailable.

Usage
# Get the pool pool = ActiveRecord::Base.connection_pool # Check pool status pool.size # => 5 pool.connections # => [#<Connection>, ...] pool.active_connection? # => true
See Also

establish_connection, connected?, remove_connection