4

I am kind of new to coding and I needed some help with one error that I'm getting.

I'm running a NodeJS application (a Discord.js bot), my bot has the function to register a user's ID to a MySQL database when the user types a certain command. Sometimes it works just fine, but other times it crashes with an ECONNRESET error:

events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on Connection instance at:
    at Connection._handleProtocolError (C:\Users\Celeron\Desktop\DiscordBot\node_modules\mysql\lib\Connection.js:423:8)
    at Protocol.emit (events.js:315:20)
    at Protocol._delegateError (C:\Users\Celeron\Desktop\DiscordBot\node_modules\mysql\lib\protocol\Protocol.js:398:10)
    at Protocol.handleNetworkError (C:\Users\Celeron\Desktop\DiscordBot\node_modules\mysql\lib\protocol\Protocol.js:371:10)
    at Connection._handleNetworkError (C:\Users\Celeron\Desktop\DiscordBot\node_modules\mysql\lib\Connection.js:418:18)
    at Socket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ECONNRESET',
  code: 'ECONNRESET',
  syscall: 'read',
  fatal: true
}
Syntle
  • 5,168
  • 3
  • 13
  • 34
Neru
  • 65
  • 1
  • 6
  • Down-voted because this question does not provide minimal reproducible code; the debugging details is also an image and not copy-pasted. See https://stackoverflow.com/help/how-to-ask – Jonathan Rosa Jun 16 '20 at 20:02
  • @JonathanRosa thanks for the info, I edited my post but the code is not in the format it should be..... – Neru Jun 16 '20 at 20:44
  • I had removed the down-vote and submitted an edit to fix the format. – Jonathan Rosa Jun 16 '20 at 20:48
  • You may want to check this related question: https://stackoverflow.com/questions/52978278/node-js-mysqland-mysql2-econnreset – Palo Jun 16 '20 at 22:57

2 Answers2

3

First of all, you should catch the error, so you app can handle it properly and doesn't crash when mysql connection is closed for any odd reason. Try either with connection.on('error', ...) or with try-catch blocks.

For keeping an open connection, you should either reconnect on close. Or simply use mysql's pooling connection, which handles automatic reconnection very well, with a single code change.

PS: Pooling multiple connections is a generally good idea for async apps, like servers, but it's safe to maintain a single connection via pooling (connectionLimit : 1) just for automatic reconnection itself.

PPS: Mysql's inactivity timeout can be configured in server's my.cnf

ΔO 'delta zero'
  • 3,506
  • 1
  • 19
  • 31
1

I fixed the problem. The crash happened because of the application inactivity on the database, and when the con. becomes inactive for a couple seconds the mysql server closes de connection making the bot to crash when the code requires some database resource.

The solution was to create a function to keep constant activity in the DB connection.

Here is the code:

    con.query('SELECT 1', (err) => {
        if (err) throw err

        console.log('Tum tum')  \\ Heartbeat noises to know if it worked xD
    })
}
setInterval(keepAlive, 15000)```
Neru
  • 65
  • 1
  • 6
  • I don't find this the very best solution, mainly because your app will still crash when it loses mysql connection for other reasons. See my answer below. – ΔO 'delta zero' Jun 17 '20 at 00:10
  • @dayvee.cz Your response indeed is much more optimal, keeping a connection open during the whole proccess brings up space for tons of errors. – Neru Jun 17 '20 at 19:24