Set up and optimize database connection pooling
Configures database connection pooling with optimized settings and implementation code.
/plugin marketplace add jeremylongshore/claude-code-plugins-plus-skills/plugin install database-deadlock-detector@claude-code-plugins-plusYou are a database connection pooling expert. Help implement efficient connection management.
Why Connection Pooling?
Pool Configuration
Best Practices
const { Pool } = require('pg');
const pool = new Pool({
host: 'localhost',
port: 5432,
database: 'mydb',
user: 'user',
password: 'password',
max: 20, // Max connections
min: 5, // Min connections
idleTimeoutMillis: 30000, // Close idle after 30s
connectionTimeoutMillis: 2000, // Wait 2s for connection
maxUses: 7500 // Recycle after 7500 uses
});
// Query with pool
async function getUser(id) {
const client = await pool.connect();
try {
const result = await client.query('SELECT * FROM users WHERE id = $1', [id]);
return result.rows[0];
} finally {
client.release();
}
}
// Or use pool.query (automatically acquires/releases)
async function getUsers() {
const result = await pool.query('SELECT * FROM users');
return result.rows;
}
from sqlalchemy import create_engine
from sqlalchemy.pool import QueuePool
engine = create_engine(
'postgresql://user:password@localhost/mydb',
poolclass=QueuePool,
pool_size=10, # Number of connections
max_overflow=20, # Additional connections if needed
pool_timeout=30, # Wait 30s for connection
pool_recycle=3600, # Recycle after 1 hour
pool_pre_ping=True # Verify connection before use
)
# Use with context manager
def get_user(user_id):
with engine.connect() as conn:
result = conn.execute("SELECT * FROM users WHERE id = %s", user_id)
return result.fetchone()
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://localhost:5432/mydb");
config.setUsername("user");
config.setPassword("password");
config.setMaximumPoolSize(20);
config.setMinimumIdle(5);
config.setConnectionTimeout(30000);
config.setIdleTimeout(600000);
config.setMaxLifetime(1800000);
HikariDataSource dataSource = new HikariDataSource(config);
// Use connection
try (Connection conn = dataSource.getConnection()) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
// Process results
}
Track these metrics:
Symptom: Timeout errors, slow requests Causes:
Solutions:
// Always release connections
const client = await pool.connect();
try {
// Your query
} finally {
client.release(); // CRITICAL
}
// Or use pool.query (auto-releases)
await pool.query('SELECT * FROM users');
Symptom: Database rejecting connections Solution: Reduce max pool size or increase database max_connections
Min: 2-5
Max: 10-20
Min: 5-10
Max: 20-50
Min: 10-20
Max: 50-100