Node-mysql where connection.end () go

I am really confused using connection.end () in node-mysysl.

I donโ€™t quite understand where it is going, at the moment I put it after the request, but then if I create a new request I get the Cannot enqueue Query after invoking quit. error message Cannot enqueue Query after invoking quit.

Now my application has a bunch of checks that go here:

  socket.on('connect', function(data,callBack){ var session = sanitize(data['session']).escape(); var query = connection.query('SELECT uid FROM sessions WHERE id = ?', [session], function(err,results){ if(err){ console.log('Oh No! '+err); }else{ io.sockets.socket(socket.id).emit('connectConfirm',{data : true}); } connection.end(); }); }); 

Now after that, if I have any other request, I get an error.

I made a more reasonable explanation in my jsFiddle: http://jsfiddle.net/K2FBk/ to better explain the problem. I am receiving. The documentation for node-mysql does not fully explain the correct placement of connection.end()

When should this error be avoided?

+8
mysql sockets
source share
2 answers

Per documentation :

Closing the connection is performed using the end () function, which ensures that all remaining requests are executed before sending the completed package to the mysql server.

connection.end() should then only be called when you stop sending requests to MySQL, that is, when your application stops. You do not have to constantly create / terminate connections: just use the same connection for all your requests (or use a more efficient connection pool).

+7
source share
 socket.on('connect', function(data,callBack){ var session = sanitize(data['session']).escape(); var query = connection.query('SELECT uid FROM sessions WHERE id = ?', [session], function(err,results){ if(err){ console.log('Oh No! '+err); }else{ io.sockets.socket(socket.id).emit('connectConfirm',{data : true}); } }); connection.end(); // close connection outside the callback }); 

Gives an error because you close the connection during en-queue

0
source share

All Articles