When to close MySQL connection using node-mysql?
I believe the proper way is to just get a connection for your app at startup, and end()
it when your app closes.
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