如果应用程序正在关闭,我将尝试关闭所有数据库连接。
但我注意到,即使在应用程序关闭之后,端口8080也没有释放。我必须手动找到保存端口的进程的id并杀死它。
很明显,我不是没有放弃db连接,就是有某种连接泄漏。但我不确定到底是什么导致了这个问题。
索引.js
const express = require('express'),
app = express(),
db = require('./db'),
port = process.env.PORT || 8080;
// Commenting out this portion of the code makes the problem go away.
// But I would Like to keep it so I can close all open database connections
// before the application is closed
process.on('SIGINT',function(){
console.log('Closing database pool');
db.pool.end();
});
routes(app);
app.listen(port);
console.log(`API Server started on localhost:${port}`);
数据库
const config = require('./config'),
mysql = require('mysql');
exports.pool = mysql.createPool(config.mysql);
关键问题:
-
-
是否存在连接泄漏等?我应该做更多的事情来更有效地处理数据库连接吗?
-
有没有更好的方法来处理应用程序崩溃或关闭的情况,并处理所有打开的连接并安全地失败?