node-mysql2: Unhandled 'error' event

I just switched to using mysql2 so I can get promise support but have an issue with pools that I didn’t have before when using mysql.

I have the following code…

const tempPool = mysql.createPool({
                    host: this.configuration[i].server,
                    user: this.credentials.user,
                    password: this.credentials.password,
                    database: 'testDB',
                    connectionLimit: connectionLimit,
                    debug: false,
                    timezone: 'utc',
                    supportBigNumbers: true,
                    bigNumberStrings: true
                });

                _this.databaseConnections[name] = tempPool;

                tempPool.getConnection().then(function (connection) {
                    connection.release();
                }).catch(function (err) {
                    console.log(err);
                });

This creates a pool, a quick connect to test and then waits. If the mySQL server restarts once connected I get the following error.

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

Error: Connection lost: The server closed the connection.
    at Socket.<anonymous> (/Users/me/Documents/code/node-archiver/node_modules/mysql2/lib/connection.js:113:35)
    at emitNone (events.js:110:20)
    at Socket.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1047:12)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)

I have tried attaching an error event handler to tempPool but still get the unhandled error exception.

Any ideas where I need to add the handler!?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 23 (9 by maintainers)

Most upvoted comments

Do you get the error when restarting the MySQL server process as well?

I’m starting to get out of my depth a little but trying to understand why it doesn’t happen in the non Promise version!