繁体   English   中英

使用node.js / mysql connection时中止连接

[英]Aborted connections when using node.js/mysql connectionPool

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应用程序时是否必须关闭连接池?

如果是,怎么办?

如果要使用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

由您决定最适合您的需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM