mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.26k stars 2.53k forks source link

Error: Cannot enqueue Handshake after already enqueuing a Handshake. #1213

Closed anonprophet closed 9 years ago

anonprophet commented 9 years ago
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

anonprophet commented 9 years ago

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

huykon commented 6 years ago

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

mozaa-vn commented 6 years ago

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));
        });
    });
});
huykon commented 6 years ago

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

innodjet commented 5 years ago

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

Amrithnath commented 5 years ago

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.

avitalBic commented 4 years ago

-> 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'
           });
});

}