1

I am getting below error every day if I left the software for more than 2 to 4 hours.

{
    code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', 
    fatal: false 
}

 

{ Error: Cannot enqueue Handshake after fatal error.
    at Protocol._validateEnqueue (/home/zubizi/retech_node_beta/node_modules/mysql/lib/protocol/Protocol.js:200:16)
    at Protocol._enqueue (/home/zubizi/retech_node_beta/node_modules/mysql/lib/protocol/Protocol.js:138:13)
    at Protocol.handshake (/home/zubizi/retech_node_beta/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/home/zubizi/retech_node_beta/node_modules/mysql/lib/Connection.js:118:18)
    at connectMysql (/home/zubizi/retech_node_beta/index.js:38:14)
    at Connection.<anonymous> (/home/zubizi/retech_node_beta/index.js:52:13)
    at Connection.emit (events.js:182:13)
    at Connection._handleProtocolError (/home/zubizi/retech_node_beta/node_modules/mysql/lib/Connection.js:425:8)
    at Protocol.emit (events.js:182:13)
    at Protocol._delegateError (/home/zubizi/retech_node_beta/node_modules/mysql/lib/protocol/Protocol.js:390:10)
    at Protocol.end (/home/zubizi/retech_node_beta/node_modules/mysql/lib/protocol/Protocol.js:116:8)
    at Socket.<anonymous> (/home/zubizi/retech_node_beta/node_modules/mysql/lib/Connection.js:97:28)
    at Socket.<anonymous> (/home/zubizi/retech_node_beta/node_modules/mysql/lib/Connection.js:502:10)
    at Socket.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1094:12)
    at args.(anonymous function) (/home/zubizi/.nvm/versions/node/v11.2.0/lib/node_modules/pm2/node_modules/event-loop-inspector/index.js:138:29)
    at process.internalTickCallback (internal/process/next_tick.js:72:19) code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false }

I tried below code to keep the connection alive. But the result is still the same.

// keep connection alive 
setInterval(function () {
    var datetime = moment.format('DD-MM-YYYY');
    conn.query('SELECT 1');
    console.log('Keep alive the connection. ' + datetime);
}, 10000);

I use this method to connect with MySQL

const conn = mysql.createConnection(config);


conn.connect(function (err) {
    if (err) {
        console.error(err);
        // process.exit(1);
    } else {
        console.log(`DB Host: ${config.host}, DB: ${config.database}`);
        console.log("Connected!");
    }
});

and if any user logs in I change the database like this:

conn.changeUser({
    database: req.session.dbname
}, function (err) {
    if (err) {
        console.log(err);
    } else {
        next();
    }
});

Is the process is wrong or there is a solution for this????

2
  • You should use connection pools. If you used sequelize, you could pass an additional parameter which would handle this task. In mysql, however, you need to do it manually as described here. I've had the same problem in my application, but fortunately, I'm using sequelize, so the fix required only a few additional lines of code. Commented May 13, 2019 at 17:44
  • @TomaszKasperczyk in pool connection how can I switch database after connecting? Commented May 14, 2019 at 6:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.