Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to close MySQL connection using node-mysql?

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

I have the following code:

var connection = mysql.createConnection({     host     : 'localhost',     user     : 'me',     password : 'secret',     database : 'Database1' }); app.put('/api/upload', function(req, res, next) {     connection.connect();     doMultipleQueries(function(err)     {         connection.end();     });           }; 

The put request works perfectly fine, but calling it the second time, I get the following error

events.js:68         throw arguments[1]; // Unhandled 'error' event                        ^ Error: Cannot enqueue Handshake after invoking quit.     at Protocol._validateEnqueue (/Users/anon/Desktop/project Web/node_modules/mysql/lib/protocol/Protocol.js:110:16) 

Am I supposed to leave the connection open until the server dies?

UPDATE: When I move the mysql.createConnection into the put request function like so:

var connection = null;  app.put('/api/upload', function(req, res, next) {     connection = mysql.createConnection({         host     : 'localhost',         user     : 'me',         password : 'secret',         database : 'Database1'     });     connection.connect();     doMultipleQueries(function(err)     {         connection.end();     });           }; 

It works fine. Does this mean connection.end() closes what mysql.createConnection created and cannot be reconnected?

like image 396
ninjaneer Avatar asked Nov 07 '12 08:11

ninjaneer


People also ask

When should I close MySQL connection?

If your script has a fair amount of processing to perform after fetching the result and has retrieved the full result set, you definitely should close the connection. If you don't, there's a chance the MySQL server will reach it's connection limit when the web server is under heavy usage.

Should I close MySQL connection after every query?

If sessions are established by different processes and connection rate is high enough you can run out of the limits with lot of the idle sessions. In that case you have to close idle connection immediately. If your MySQL server is used by local web-server than you can connect to it via file-socket instead of IP-socket.

How do I close a node connection in MySQL?

To close a database connection gracefully, you call the end() method on the connection object. The end() method ensures that all remaining queries are always executed before the database connection closed. To force the connection close immediately, you can use the destroy() method.

Should I close DB connection after query?

For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.


2 Answers

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

like image 59
metalfight - user868766 Avatar answered Oct 13 '22 00:10

metalfight - user868766


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

like image 23
Chris Avatar answered Oct 12 '22 22:10

Chris