Reputation: 43973
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
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
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:
Upvotes: 2