node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Can't add new command when connection is in closed state

Open Shubh0817 opened this issue 1 year ago • 1 comments

I am using mysql2 driver to connect MySql Database with below config

mysql.createPool({ host: process.env.NODE_MYSQL_IP, user: process.env.MYSQL_USER, password: decryptPassword(process.env.NODE_MYSQL_PASSWORD), database: process.env.MYSQL_SCHEMA, waitForConnections: true, connectionLimit: 4, // 20 concurrent connections maxIdle: 4, // max idle connections, the default value is the same as connectionLimit idleTimeout: 20000, // idle connections timeout, in milliseconds, the default value 60000 queueLimit: 0 });

I am using execute method instead of query.

when i have to just perform select query then i directly use pool but when i have to start transaction then i get connection from pool and then commit and release respectively.

also i have printed free connection with all connection as below code

const allConnections = connection.pool?._allConnections.length ?? connection.connection._pool?._allConnections.length; const freeConnections = connection.pool?._freeConnections.length ?? connection.connection._pool?._freeConnections.length;

my allConnections become 0 after some time and then i got error as Error: Can't add new command when connection is in closed state on execute method.

please help

Shubh0817 avatar Mar 14 '24 12:03 Shubh0817

maybe is your MYSQL config has time_out?

qddtdlko avatar Jul 02 '24 16:07 qddtdlko