MYSQL2 Dialect - Connection not disposed after a connection error during query execution
See original GitHub issueEnvironment
Knex version: 0.21.21 (same issue on 0.95.12) Database + version: MYSQL 5.7 OS: Tested on Windows 10 and Linux Ubuntu 20.04.2
Bug
- When there is a connection issue (closed connection) during a query execution, the connection does not get removed from the pool and continues to be used, causing all queries executed after to fail with connection is closed error. The only way to recover from the issue is to restart the app. Note this does not happen on mysql dialect, only on Mysql2 dialect.
MYSQL2 does not emit an error event, when there is a connection issue while executing a query, but returns a fatal=true property in the exception. The dialect should handle this as a fatal error and set the __knex__disposed ?
-
Error: Can’t add new command when connection is in closed state
-
Example Code (needs a mysql server)
const knex = require('knex')({
client: 'mysql2',
connection: {
host : '127.0.0.1',
port : 3306,
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
},
pool: { min: 0, max: 7 }
});
const main = async function()
{
try {
await knex.raw("select sleep(100)");
// !! Manually KILL Connection from MYSQL to simulate connection error => KILL <pid>
} catch(err) {
console.log(err);
}
// all queries from now on will return error when reusing the same connection in the pool
try {
await knex.raw("select 1");
} catch(err) {
console.log(err);
}
process.exit();
}
main();
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
Three bugs in the Go MySQL Driver | The GitHub Blog
Query would never fail with an “invalid connection” error, because even if the library attempted to perform the query in an invalid connection, ......
Read more >2 Server Error Message Reference - MySQL :: Developer Zone
Possible causes: Permissions problem for source file; destination file already exists but is not writeable. Error number: 1005 ; Symbol: ER_CANT_CREATE_TABLE ; ...
Read more >nodejs mysql Error: Connection lost The server closed the ...
In your code i am missing the parts after connection = mysql. ... that part of program logic is executed using the same...
Read more >Working with Engines and Connections — SQLAlchemy 2.0 ...
Engine Disposal; Working with Driver SQL and Raw DBAPI Connections ... SQL string that is passed to the database only, and not the...
Read more >Creating database connections - Do it once or for each query?
If you open a connection when you need it and dispose of it when you've finished, that will not actually close the connection,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fix released in 0.95.14
@christianjank thanks for the solution, it’s an easy fix.
I was just on it when you send your message.