question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MYSQL2 Dialect - Connection not disposed after a connection error during query execution

See original GitHub issue

Environment

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

  1. 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 ?

  1. Error: Can’t add new command when connection is in closed state

  2. 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:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
kibertoadcommented, Nov 8, 2021

Fix released in 0.95.14

1reaction
OlivierCavadenticommented, Nov 8, 2021

@christianjank thanks for the solution, it’s an easy fix.

@davidf84 Would you consider sending PR for this issue?

I was just on it when you send your message.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found