ActiveRecord::Base#connection_pool
⏳ Pending Review
📁 Source: activerecord/lib/active_record/connection_adapters/pool.rb
Reached consensus on Feb 10, 202622 total votes
A Version A ⭐ Winner
14 votes (64%)
B Version B
5 votes (23%)
C Version C
3 votes (14%)
🏆 Winner — Version A
connection_pool → ConnectionPool

Description

Returns the connection pool associated with this class. The connection pool manages a set of reusable database connections, providing thread-safe checkout and checkin operations. Each connection is allocated to a requesting thread and returned to the pool when no longer needed, ensuring efficient resource usage under concurrent load.

The pool is lazily initialized on first access and its size is determined by the pool option in your database.yml configuration. When all connections are checked out and a new request arrives, the pool will block for up to checkout_timeout seconds before raising a ConnectionTimeoutError.

Parameters

  • None — This is a reader method with no parameters.

Returns

ActiveRecord::ConnectionAdapters::ConnectionPool — The connection pool instance for this class's database configuration.

Examples

# Access the connection pool pool = ActiveRecord::Base.connection_pool # Check pool statistics pool.size # => 5 (default) pool.connections # => [#<ConnectionAdapter>, ...] pool.active_connection? # => true # Use with_connection for automatic checkout/checkin ActiveRecord::Base.connection_pool.with_connection do |conn| conn.execute("SELECT 1") end # Retrieve pool stats stats = pool.stat # => { size: 5, connections: 2, busy: 1, dead: 0, idle: 1, waiting: 0, checkout_timeout: 5 }
All Versions (3)
Version A 14 votes

Returns the connection pool associated with this class. The connection pool manages a set of reusable database connections, providing thread-safe checkout and checkin operations...

The pool is lazily initialized on first access and its size is determined by the pool option in your database.yml.

Version B 5 votes

The connection pool acts as a thread-safe intermediary between your application and the database layer. Think of it as a checkout counter: threads borrow a connection, use it, and return it when done.

Access via ActiveRecord::Base.connection_pool. Configure pool size in database.yml.

Version C 3 votes

#connection_pool — Returns the ConnectionPool instance. Manages reusable database connections. Key methods: checkout, checkin, with_connection.

Configure via pool size setting in database.yml. Raises ConnectionTimeoutError if pool exhausted.

Admin Decision
Approving will automatically submit a PR to rails/rails