chill117 / express-mysql-session

A MySQL session store for the express framework in node
MIT License
313 stars 109 forks source link

PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR #110

Closed FelixVincent99 closed 4 years ago

FelixVincent99 commented 4 years ago

Can someone help me with this:

{
  code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR',
  fatal: false
}
function handleDisconnect() {
    connection = mysql.createConnection(db_config); 
    connection.connect(function(err) {           
      if(err) { 
        console.log('error when connecting to db:', err);
        setTimeout(handleDisconnect, 2000); 
      }
      console.log("Database Connected!");
    });
    connection.on('error', function(err) {
      console.log('db error', err);
      if(err.code === 'PROTOCOL_CONNECTION_LOST') { 
        handleDisconnect();                         
      } else {  
        throw err;
      }
    });
  }

  handleDisconnect();
chill117 commented 4 years ago

You may lose the connection to a MySQL server due to network problems, the server timing you out, the server being restarted, or crashing. All of these events are considered fatal errors, and will have the err.code = 'PROTOCOL_CONNECTION_LOST'. See the Error Handling section for more information.

Re-connecting a connection is done by establishing a new connection. Once terminated, an existing connection object cannot be re-connected by design.

Using a connection object after a fatal error will result in the error that you are experiencing. You should not try to manually reconnect a connection object that has been disconnected or resulted in a fatal error.

I suggest that you use pooling instead of a single connection. Specifically because:

With Pool, disconnected connections will be removed from the pool freeing up space for a new connection to be created on the next getConnection call.

See Server disconnects and Pooling connections for more information.