mysql-events icon indicating copy to clipboard operation
mysql-events copied to clipboard

Reconnection after connection lost

Open ubuntutest opened this issue 4 years ago • 5 comments

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

ubuntutest avatar May 21 '20 12:05 ubuntutest

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

brijeshpant83 avatar Aug 31 '20 12:08 brijeshpant83

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.

edilsonlm217 avatar Sep 04 '20 21:09 edilsonlm217

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() } })) }

8secz-johndpope avatar Oct 01 '20 02:10 8secz-johndpope

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.

muriloneo avatar Oct 05 '21 12:10 muriloneo

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....

8secz-johndpope avatar Oct 13 '21 06:10 8secz-johndpope