Skip to content

Commit

Permalink
Recognize fatal mysql2 connection errors
Browse files Browse the repository at this point in the history
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 knex#1853
  • Loading branch information
novemberborn committed Jul 28, 2017
1 parent 03f0fb4 commit 67f8deb
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/dialects/mysql2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 67f8deb

Please sign in to comment.