Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

I don't fully understand where it goes, at the moment i place it after a query but then if i create a new query i get an error Cannot enqueue Query after invoking quit.

Now my app has a bunch of checks going here is one of them:

   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 query i get the error occuring.

I made a more informed explaination in my jsFiddle: http://jsfiddle.net/K2FBk/ to better explain the problem I'm getting. The documentation for node-mysql does not totally explain the correct place to put connection.end()

Where should it go to avoid this error?

Closing the connection is done using end() which makes sure all remaining queries are executed before sending a quit packet to the mysql server.

connection.end() is then supposed to be called only when you stop sending queries to MySQL, i.e. when your application is stopping. You shouldn't create/end connections all the time: just use the same connection for all your queries (or use a connection pool to be more efficient).

Thanks for the reply, so in my JSFiddle code would i simply put it on line number 75? =/ Or does it still have to be within the app connection... this is what i found confusing. – Sir Dec 19, 2013 at 22:44 You'd close the connection when your application stops. If you stop it using the control-c keys, then look at this answer and put the connection.close() there. – Paul Mougel Dec 19, 2013 at 22:58 @PaulMougel can you please help on this thread, I'm using a singleton pattern for the mysqlconnection, I never call end on connection and keep on reusing it throughout the life of the app, however, I'm concerned with timeout setting, i.e. mysql killing the connection on inactivity stackoverflow.com/questions/26533936/… – user2727195 Oct 23, 2014 at 17:55
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

Its giving error because you are closing connection during en-queue

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.