When to close MySQL connection using node-mysql?

前端 未结 2 867
予麋鹿
予麋鹿 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: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

提交回复
热议问题