Connection Pool
The ODY framework implements a robust database connection pool that leverages Swoole’s coroutines for high-performance database access. Here’s an overview of how it works:
Core Concepts
The connection pool manages a collection of database connections that can be borrowed, used, and returned without the overhead of establishing new connections for each database operation.
Key Features
- Coroutine-Aware: Binds connections to Swoole coroutines for automatic management
- Connection Reuse: Maintains a pool of pre-established connections to reduce connection overhead
- Auto-Return: Automatically returns connections to the pool when coroutines complete
- Configurable Sizing: Adjustable pool size based on workload requirements
- Connection Health Checks: Validates connections before they’re borrowed
- Leak Detection: Identifies and logs potential connection leaks
- Connection Lifecycle Management: Handles connection creation, validation, and cleanup
How It Works
Initialization:
ConnectionManager::initPool($config, $name);
This creates a new connection pool with the specified configuration.
Borrowing a Connection:
$connection = ConnectionManager::getConnection($name);
This gets a PDO connection from the pool, binding it to the current coroutine.
Auto-Return: When the coroutine completes, the connection is automatically returned to the pool if
autoReturn
is enabled.Manual Return: If
autoReturn
is disabled, you must explicitly return connections:ConnectionManager::getPool($name)->return($connection);
Pool Management: The pool automatically scales between minimum idle connections and maximum size based on demand.
Configuration Options
size
: Maximum number of connections in the poolminimumIdle
: Minimum number of idle connections to maintainidleTimeoutSec
: Time after which idle connections are removedmaxLifetimeSec
: Maximum lifetime of a connection before recreationborrowingTimeoutSec
: Maximum time to wait when borrowing a connectionautoReturn
: Whether to automatically return connectionsbindToCoroutine
: Whether to bind connections to coroutines
Connection Lifecycle
- Creation: New connections are created as needed up to the pool size limit
- Validation: Before a connection is borrowed, it’s checked for validity
- Usage: The connection is marked as in-use while borrowed
- Return: The connection is returned to the pool after use
- Maintenance: Idle connections are periodically checked and renewed
- Cleanup: Connections are properly closed when the pool is shutdown
By using this connection pool, database operations become more efficient, especially under high concurrency, as connection creation overhead is minimized.