Aborted connections when using node.js/mysql connectionPool

扶醉桌前 提交于 2020-01-05 09:03:33

问题


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();
});

I don't see any warnings in the output, but on the MySQL server I see aborted connections since testing with this node.js code.

Do I have to close the connection pool when I stop the node.js app?

If yes, how?


回答1:


If you're closing you're node.js app with a Ctrl+C command, you could close your connection pool on the SIGINT event:

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);
  });
});

But you could also configure your MySQL server to close idle connections, setting the server variables wait_timeout and/or interactive_timeout.

It's up to you to decide what best fits your needs.



来源:https://stackoverflow.com/questions/23912255/aborted-connections-when-using-node-js-mysql-connectionpool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!