express-mysql-session
express-mysql-session copied to clipboard
PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR with express-mysql-session
Hello,
I have followed https://stackoverflow.com/a/20211143/5224768 to fix PROTOCOL_CONNECTION_LOST
which worked but now I am getting PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR
Here is a snapshot of my code:
let connection: mysql.Connection;
function handleDisconnect() {
connection = mysql.createConnection(dbConfig); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function (err) {
// The server is either down
if (err) {
// or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function (err) {
winstonLogger.error('db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
// Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else {
// connection idle timeout (the wait_timeout
winstonLogger.error('Unexpected database connection error', err);
// throw err; // server variable configures this)
}
});
}
handleDisconnect();
/**
* Session storage configuration
*/
const sessionStore = new mySQLStore({} /* session store options */, connection);
app.use(
session({
store: sessionStore,
....
}),
);
I know I am supposed to use connection pooling based on this comment https://github.com/chill117/express-mysql-session/issues/110#issuecomment-641212842
But I honestly don't know how to do this here. Can someone please help?
I am trying:
const pool = mysql.createPool({
...dbConfig,
connectionLimit: 10,
});
/**
* Session storage configuration
*/
const sessionStore = new mySQLStore({} /* session store options */, pool );
app.use(
session({
store: sessionStore,
....
}),
);
Hope this works.
I think you should not be trying to handle disconnects in your application code. The mysql pool handles this automatically. The error ("PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR") means you're trying to use a mysql connection after it has already had a fatal error and disconnected. The pool object will discard such connections and open new ones automatically. So just use the pool object as needed for your db calls and disconnects/reconnects are handled for you.
I have the same issues ... {"code":"PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR","fatal":false}....
please give a solution
The latest release of this module uses the mysql2 module. By default, a connection pool will be used. This will handle connection/reconnection automatically so that you don't need to worry about that in your application code. Please see the readme for usage details.