我在 Nodejs 上使用 MySQL。我正在使用 mysql pool
创建连接:
Var mysqk = require ('mysql');
Var pool = mysql.createPool ({my configs});
我的问题是:
我将在应用中的哪个位置使用文档中报告的 pool.end()
?
例如:在我的 www
文件中,我创建了一个代码来释放我在代码中指出的其他内容。我应该在那里使用 pool.end()
吗?
var app = require('../app');
var debug = require('debug')('cancela:server');
var http = require('http');
var port = normalizePort(process.env.PORT || '4000');
app.set('port', port);
var server = http.createServer(app);
// my app codes...
..
.
.
process.once('SIGTERM', end);
process.once('SIGINT', end);
function end() {
server.close(function(err){
if(err) throw err();
// Should I end the Pool connection here? <<<<<<===================================
// pool.end(function (err) {
// // all connections in the pool have ended
// });
console.log('Server endded!');
/* exit gracefully */
process.exit();
});
}
最佳答案
有时您出于某种原因想要手动释放连接池,以便您可以在最终重新创建连接池之前使用 pool.end()
。
大多数时候你想在你的服务器即将关闭时释放它,这样你就可以做你想做的事。注意函数调用process.exit()
的位置。必须在连接释放后调用,否则池无法释放完毕。
function end() {
server.close(function (err) {
if (err) throw err;
console.log('Server endded!');
pool.end(function (err) {
if (err) throw err;
process.exit();
});
});
}
关于mysql - 在哪里结束nodejs上的mysql池连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45289797/