var mysql = require('mysql');
var mysqlPool = mysql.createPool({
host : 'localhost',
user : 'nodejs',
password: '',
database: 'nodejs'
});
// somewhere inside a method:
mysqlPool.getConnection(function(err, connection) {
// do some queries
connection.release();
});
我没有在输出中看到任何警告,但是在MySQL服务器上,我看到中止连接,因为使用此node.js代码进行测试。
停止node.js应用时,是否必须关闭连接池?
如果是,怎么样?
答案 0 :(得分:1)
如果您使用Ctrl+C
命令关闭了node.js应用,则可以关闭SIGINT
事件中的连接池:
process.on('SIGINT', function() {
mysqlPool.end(function (err) {
/* Since you're overriding the default behavior of SIGINT,
you have to force your app to exit. You can pass it as
a callback to the end() function. */
process.exit(0);
});
});
但您也可以将MySQL服务器配置为关闭空闲连接,设置服务器变量wait_timeout
和/或interactive_timeout
。
由您来决定哪种最适合您的需求。