Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@davidf84

Description

@davidf84

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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions