Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

255
Visualizações
PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR After 8 PM

I have a problem about connecting to MySQL. After 8 PM (My local time), the connection is lost. So I modified my code, whenever mysql throw PROTOCOL_CONNECTION_LOST, it will try to reconnect to the mysql. The error for PROTOCOL_CONNECTION_LOST is gone, but it give new error code PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR when trying to connect to the server. This is my code to create connection to MySQL.

const dbConn = mysql.createConnection({
    host        : 'localhost',
    user        : 'xxxx',
    password    : 'xxxx',
    database    : 'test'
});

function handleDisconnect() {

  dbConn.connect(function(err, connection) {             
    if(err) {                                     
      console.log('error when connecting to db:', err);
      setTimeout(handleDisconnect, 2000); 
    }       
  });                                     
   
  dbConn.on('error', function(err) {
    console.log('db error', err);
    if(err.code === 'PROTOCOL_CONNECTION_LOST') { 
      handleDisconnect();                         
    } else {                                     
      throw(err)                       
    }
  });
}

handleDisconnect();
module.exports = dbConn;

This is my error stack

0|index    | error when connecting to db: Error: Cannot enqueue Handshake after fatal error.
0|index    |     at Protocol._validateEnqueue (/home/mydev/SOBATku/node_modules/mysql/lib/protocol/Protocol.js:212:16)
0|index    |     at Protocol._enqueue (/home/mydev/SOBATku/node_modules/mysql/lib/protocol/Protocol.js:138:13)
0|index    |     at Protocol.handshake (/home/mydev/SOBATku/node_modules/mysql/lib/protocol/Protocol.js:51:23)
0|index    |     at Connection.connect (/home/mydev/SOBATku/node_modules/mysql/lib/Connection.js:116:18)
0|index    |     at handleDisconnect (/home/mydev/SOBATku/config/db.config.js:34:10)
0|index    |     at Connection.<anonymous> (/home/mydev/SOBATku/config/db.config.js:45:7)
0|index    |     at Connection.emit (node:events:390:28)
0|index    |     at Connection._handleProtocolError (/home/mydev/SOBATku/node_modules/mysql/lib/Connection.js:423:8)
0|index    |     at Protocol.emit (node:events:390:28)
0|index    |     at Protocol._delegateError (/home/mydev/SOBATku/node_modules/mysql/lib/protocol/Protocol.js:398:10) {
0|index    |   code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR',
0|index    |   fatal: false
0|index    | }

It can reconnect when I do some command like pm2 logs or other command with pm2 (I use pm2 for node run command). Planning to use cron function to restart my service every 8PM, but if it can solved without restart the service, it will be very helpful. Thank you.

about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda