-
-
Notifications
You must be signed in to change notification settings - Fork 645
Description
Knex.js needs to validate mysql2 connections before use in connection pools. Currently, we're forced to use private properties that could break with any update.
Current Situation
// Knex's mysql2 dialect - relies on private properties
validateConnection(connection) {
return (
connection &&
!connection._fatalError && // Private
!connection._protocolError && // Private
!connection._closing && // Private
!connection.stream.destroyed
);
}
For comparison, the original mysql
driver provides public API:
// mysql driver - uses public 'state' property
validateConnection(connection) {
return connection.state === 'connected' || connection.state === 'authenticated';
}
Background
In 2017, you kindly offered to add public API for this (knex#2175):
I'm happy to add anything that would work for you, ideally in a way compatible with mysqljs/mysql
what if I wrap_fatalError
into a getter function and "bless" it to be public api?
This would still be incredibly helpful for connection pool management.
Request
Could mysql2 provide one of these public methods?
Option A: Simple boolean check
connection.isValid() // returns true/false
Option B: State property (mysql compatible)
connection.state // 'connected' | 'disconnected' | 'protocol_error' | 'closing'
Option C: Expose existing checks
connection.hasFatalError() // or
connection.hasProtocolError() // or
connection.isClosing()
Related Issues
- MySQL2 dialect relies on private API for connection validation knex/knex#6255 - MySQL2 relies on private API for connection validation
- Unhandled Exception in
connection.end()
for MySQL Dialect knex/knex#1798 - Original connection handling issue - Recognize fatal mysql2 connection errors knex/knex#2175 - Previous attempt to improve validation
Would you consider adding any of these public methods? Option B would maintain compatibility with the original mysql driver, making migration easier for users.
Thank you for maintaining this excellent library!