Mysql: Error: Cannot enqueue Handshake after already enqueuing a Handshake.

Created on 7 Sep 2015  路  7Comments  路  Source: mysqljs/mysql

function reloadQuestion() {
    var obj = {};
    connection.connect();
    connection.query("SELECT * FROM quiz", function (err, rows, fields) {
        if (err) throw err;
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            obj = {"id":row.id, "question":row.question, "answers":row.answers.split(", ")};
            f100.push(obj);
            arr.push(i);
        };
        shuffleArray(arr);
        newQuestion();
    });
    connection.end();
}

reloadQuestion();

its work fine, then when after a condition when try to call reloadQuestion again its got error

Error: Cannot enqueue Handshake after already enqueuing a Handshake.
    at Protocol._validateEnqueue (/home/bot/node_modules/mysql/lib/protocol/Protocol.js:202:16)
    at Protocol._enqueue (/home/bot/node_modules/mysql/lib/protocol/Protocol.js:129:13)
    at Protocol.handshake (/home/bot/node_modules/mysql/lib/protocol/Protocol.js:52:41)
    at Connection.connect (/home/bot/node_modules/mysql/lib/Connection.js:123:18)
    at reloadQuestion (/home/bot/f100.js:56:13)
    at null._onTimeout (/home/bot/f100.js:80:8)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

already try to remove connection.end(); but the problem still

question

Most helpful comment

I think use pooling is the best way
For example:

var pool        = mysql.createPool({
    connectionLimit : 10, // default = 10
    host            : 'localhost',
    user            : 'root',
    password        : 'kensxxx',
    database        : 'fbtool'
});

router.get('/test/query', function (req, res) {
    pool.getConnection(function (err, connection) {
        connection.query("SELECT * FROM tblcomment", function (err, rows) {
            connection.release();
            if (err) throw err;

            console.log(rows.length);
            res.send(JSON.stringify(rows));
        });
    });
});

All 7 comments

im fixed this using connection pool remove .connect() & . end() and using .release

I have same problem although I am not using .connect() & . end() .
Anyone help me? Thanks

I think use pooling is the best way
For example:

var pool        = mysql.createPool({
    connectionLimit : 10, // default = 10
    host            : 'localhost',
    user            : 'root',
    password        : 'kensxxx',
    database        : 'fbtool'
});

router.get('/test/query', function (req, res) {
    pool.getConnection(function (err, connection) {
        connection.query("SELECT * FROM tblcomment", function (err, rows) {
            connection.release();
            if (err) throw err;

            console.log(rows.length);
            res.send(JSON.stringify(rows));
        });
    });
});

Hi @MrKenitvnn , Still not working with me :( . Any way help me check it?

I had the same issue on my end and I resolve it by removing
connection.connect(); and connection.end();

Hey guys Use mysql.createPool instead of mysql.createConnection to set up teh conenction and use connection.release to end connections.

This error happens when you have multiple queries running through the program and the connections aren't closed properly after the query is resolved.

-> I resolve this so that every time after connecting to the DB (Query etc.) I close the connection using this function:
(The function terminates the connection and creates the connection again)


function endCon(){
db.end( function(err) {
if (err) {console.log("Error ending the connection:",err);}

     db = mysql.createConnection({
        host: 'localhost',
        user: 'your_user',
        password: 'your_psw',
        database: 'your_DB'
           });
});

}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tbaustin picture tbaustin  路  3Comments

JCQuintas picture JCQuintas  路  3Comments

nanom1t picture nanom1t  路  3Comments

ajpyoung picture ajpyoung  路  4Comments

whatthehell232 picture whatthehell232  路  3Comments