From 337178fbff78bfd8a138138a93266d88edab6c5e Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Mon, 8 Nov 2021 22:12:34 +0100 Subject: [PATCH] Mysql2 validate connection fix #4794 (#4812) (cherry picked from commit 89bd0a034fc22413a1f8328bb09193d0a1b5d113) --- lib/dialects/mysql/index.js | 10 +++------- lib/dialects/mysql2/index.js | 11 +++++++---- test/tape/pool.js | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/dialects/mysql/index.js b/lib/dialects/mysql/index.js index d4eb4efb13..ddace51d0d 100644 --- a/lib/dialects/mysql/index.js +++ b/lib/dialects/mysql/index.js @@ -89,13 +89,9 @@ class Client_MySQL extends Client { } validateConnection(connection) { - if ( - connection.state === 'connected' || - connection.state === 'authenticated' - ) { - return true; - } - return false; + return ( + connection.state === 'connected' || connection.state === 'authenticated' + ); } // Grab a connection, run the query via the MySQL streaming interface, diff --git a/lib/dialects/mysql2/index.js b/lib/dialects/mysql2/index.js index bc0bd8fd76..63bfa4f082 100644 --- a/lib/dialects/mysql2/index.js +++ b/lib/dialects/mysql2/index.js @@ -15,10 +15,13 @@ class Client_MySQL2 extends Client_MySQL { return require('mysql2'); } validateConnection(connection) { - if (connection._fatalError) { - return false; - } - return true; + return ( + connection && + !connection._fatalError && + !connection._protocolError && + !connection._closing && + !connection.stream.destroyed + ); } } diff --git a/test/tape/pool.js b/test/tape/pool.js index 7019bc0bd0..1780dad17f 100644 --- a/test/tape/pool.js +++ b/test/tape/pool.js @@ -23,7 +23,7 @@ test(`pool evicts dead resources when factory.validate rejects`, async (t) => { }, validate: (res) => { - return res.error ? false : true; + return !res.error; }, destroy: (res) => {