Skip to content

Reconnection after connection lost #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ubuntutest opened this issue May 21, 2020 · 5 comments
Open

Reconnection after connection lost #26

ubuntutest opened this issue May 21, 2020 · 5 comments

Comments

@ubuntutest
Copy link

Hi, congratulations for this module, I use it in production on my platform.

I use pm2 as a process manager.

Sometimes I get the following error:

{ Error: Connection lost: The server closed the connection.
    at Protocol.end (/home/imroot/node_modules/mysql/lib/protocol/Protocol.js:112:13)
    at Socket.<anonymous> (/home/imroot/node_modules/mysql/lib/Connection.js:94:28)
    at Socket.<anonymous> (/home/imroot/node_modules/mysql/lib/Connection.js:526:10)
    at Socket.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19) fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }

I think it's because my app occasionally disconnects from the database.

I found this and much more, but I'm not sure how to adapt it to your model with this different syntax, could you help me out? it could be useful for many.

Thanks

@brijeshpant83
Copy link

seems I am having same problem, sometime its get disconnected and unable to reconnect. pls suggest.

@edilsonlm217
Copy link

I'm also getting an error like that. I'm watching for those database events to notify users and at some point it stops working.

@8secz-johndpope
Copy link

I will detail some designs - but I've introduced a health check in another process that causes a restart on pm2 if the service fails.
// var mysqlPool = mysql.createPool(config.db);
var connection = mysql.createConnection(config.db);

async function getConnection() {
await new Promise ( resolve => mysqlPool.getConnection( async (err, connection) => {
if(err){
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.error('👺 Database connection was closed.')
}
if (err.code === 'ER_CON_COUNT_ERROR') {
console.error('👺 Database has too many connections.')
}
if (err.code === 'ECONNREFUSED') {
console.error('👺 Database connection was refused.')
}
console.error('👺 mysqlPool.getConnection: ', new Date(), JSON.stringify(err))
// await this.getConnection()
} else {
console.log('🎃 connected connection:',connection)
connection = connection
resolve()
}
}))
}

@muriloneo
Copy link

does anyone has found a way to keep connected ? I have a problem that the connection can break on the middle of a processing that is activated by a insert trigger.

@8secz-johndpope
Copy link

I use dynamodb to pick up from the last known rowid / timestamp that's been processed.
https://gist.github.com/8secz-johndpope/4bf52a0d40071bfa75bfea35aea7f5b9
On reconnect - I just flash forward through transactions....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants