From 96615c85b2395c1a8f7b4a3bb547a31dce75d341 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Fri, 28 Jul 2017 16:51:15 +0100 Subject: [PATCH] Recognize fatal mysql2 connection errors Connections created by MySQL2 driver transition to broken state when network errors or timeouts occur. This condition is not visible to the connection pool in Knex. If Knex detects a timeout and tries to close a connection, this causes an uncaught exception from MySQL2. Similarly the connection pool never realizes the connection is in a broken state, and thus will never replace it. Unfortunately the MySQL2 connection does not expose a state, but it can be inferred by checking for `connection._fatalError`. Fixes #1798 --- src/dialects/mysql2/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/dialects/mysql2/index.js b/src/dialects/mysql2/index.js index 3c1c225fa0..79e832192c 100644 --- a/src/dialects/mysql2/index.js +++ b/src/dialects/mysql2/index.js @@ -29,8 +29,8 @@ assign(Client_MySQL2.prototype, { return require('mysql2') }, - validateConnection() { - return true + validateConnection(connection) { + return !connection._fatalError }, // Get a raw connection, called by the `pool` whenever a new @@ -50,6 +50,10 @@ assign(Client_MySQL2.prototype, { }) }, + destroyRawConnection(connection) { + if (!connection._fatalError) return Client_MySQL.prototype.destroyRawConnection(connection) + }, + processResponse(obj, runner) { const { response } = obj const { method } = obj