Overview
Features
| Feature | Description |
|---|---|
| Thread-safe | Safe for concurrent access from multiple threads |
| Auto-scaling | Creates connections up to max_size on demand |
| Health checks | Validates connections before use |
| Connection aging | Recreates old connections automatically |
| Statistics | Monitors pool performance |
Configuration
ConnectionPoolConfig
Configuration Options
| Option | Default | Description |
|---|---|---|
min_size | 2 | Minimum pool size (pre-created) |
max_size | 10 | Maximum pool size |
timeout | 5.0 | Checkout timeout in seconds |
max_connection_age | 3600 | Max connection age in seconds |
check_same_thread | False | SQLite threading check |
sqlite_timeout | 30.0 | SQLite busy timeout in seconds |
Basic Usage
Context Manager
Pool Lifecycle
As Context Manager
Thread Safety
Multi-threaded Usage
Thread Isolation
By default,check_same_thread=False allows connections to be used across threads:
Connection Health
Automatic Health Checks
The pool validates connections before checkout and on return:Connection Recreation
Old or unhealthy connections are automatically recreated:Error Handling
Timeout Handling
Database Errors
Statistics and Monitoring
Get Pool Stats
Stats Fields
| Field | Description |
|---|---|
pool_size | Current total connections |
active_connections | Total connections created (including in-use and available) |
available_connections | Connections ready for checkout |
total_checkouts | Total checkout operations |
average_wait_time_ms | Average wait for connection |
peak_active | Maximum concurrent checkouts |
min_size | Configured minimum pool size |
max_size | Configured maximum pool size |
Monitoring Example
Integration with Providers
SQLiteProvider Usage
SpellChecker Integration
Performance Tuning
Pool Sizing
| Scenario | min_size | max_size | Notes |
|---|---|---|---|
| Single-threaded | 1 | 1 | No pooling needed |
| Light multi-threaded | 2 | 5 | Default configuration |
| Heavy multi-threaded | 5 | 20 | Web server workloads |
| Batch processing | 1 | CPU cores | Match worker threads |
Timeout Tuning
Connection Age
PooledConnection
Internal connection wrapper with metadata:Best Practices
Always Use Context Manager
Close Pool on Shutdown
Monitor Pool Usage
Handle Exhaustion Gracefully
See Also
- Configuration Guide - ConnectionPoolConfig options
- Providers - Database providers
- Performance Tuning - Optimization strategies
- Logging - Connection pool logging