omega
omega

Reputation: 43973

node js mysql Cannot Enqueue a query after calling quit

I am using node.js and mysql, to run sql commands frequently.

I am getting this error: Cannot enqueue Handshake after invoking quit.

This is when I do

connection = mysql.createConnection(sqlDetails);
connection.connect();

and

connection.end();

If I comment the .connect and .end lines, then I get the error ER_CON_COUNT_ERROR: Too many connections.

Does anyone know how to fix this problem?

Thanks

Upvotes: 1

Views: 11706

Answers (2)

keerthee
keerthee

Reputation: 880

Since node js is asynchronous , connection.connect() and connection.end() gets executed parallely,

If you want to specify sequence of execution, use callbacks like

connection = mysql.createConnection(sqlDetails);
connection.connect(function(err,callback){
    connection.query(function(err,callback){
         connection.end();
 });
});

Upvotes: 2

Andrey Sidorov
Andrey Sidorov

Reputation: 25466

If you actually want to close connection (why? if you 'run sql commands frequently' you probably want to reuse it, or even better - use connection pool) you need to do that in query result callback. All commands are put into commands queue and processed sequentially.

This is what you are doing:

  • open connection stream to mysql
  • add Handshake command to queue
  • add Query command to queue
  • add Quit command to queue, mark connection as closing
  • start processing commands
  • now Handshake is processed when connection in 'closing' state

Upvotes: 2

Related Questions