When to close MySQL connection using node-mysql?

前端 未结 2 868
予麋鹿
予麋鹿 2020-12-30 20:59

Currently using: https://github.com/felixge/node-mysql

I have the following code:

var connection = mysql.createConnection({
    host     : \'localhos         


        
相关标签:
2条回答
  • 2020-12-30 21:19

    I believe the proper way is to just get a connection for your app at startup, and end() it when your app closes.

    0 讨论(0)
  • 2020-12-30 21:35

    connection.end() does it job regardless to fatal error. If a fatal error occurs before the COM_QUIT packet can be sent, an err argument will be provided to the callback, but the connection will be terminated regardless of that.

    Also check destroy() method. This will cause an immediate termination of the underlying socket.

    You can add error handler.

    https://github.com/felixge/node-mysql/blob/master/Readme.md#error-handling

    // I am Chuck Norris:
    connection.on('error', function() {});
    

    Once terminated, an existing connection object cannot be re-connected by design.

    Check here. It's showing connecting back after disconnect.

    https://github.com/felixge/node-mysql/blob/master/Readme.md#server-disconnects

    0 讨论(0)
提交回复
热议问题