Always properly manage database connections to prevent resource leaks and improve application stability. Implement these key practices: 1. Release connections back to the pool after use with a try/finally block
Always properly manage database connections to prevent resource leaks and improve application stability. Implement these key practices:
Example:
// Connection pool setup with proper configuration
const dbPool = mysql.createPool({
...dbConfig,
connectionLimit: 10,
queueLimit: 0,
connectTimeout: 10000,
});
// Function using a connection with proper release
async function queryDatabase() {
const connection = await dbPool.getConnection();
try {
const [results] = await connection.execute('SELECT id, name FROM users WHERE status = ?', ['active']);
return results;
} catch (error) {
console.error('Database query failed:', error);
throw error;
} finally {
// Always release the connection back to the pool
connection.release();
}
}
// Graceful shutdown handler
process.on('SIGTERM', async () => {
console.log('Closing database connections...');
if (dbPool) {
await dbPool.end();
console.log('Database pool closed');
}
process.exit(0);
});
Enter the URL of a public GitHub repository