mariadb-corporation / mariadb-connector-nodejs

MariaDB Connector/Node.js is used to connect applications developed on Node.js to MariaDB and MySQL databases. MariaDB Connector/Node.js is LGPL licensed.
GNU Lesser General Public License v2.1
366 stars 91 forks source link

I get the error SqlError: Cannot execute new commands: connection closed. #204

Closed dotoritos-kim closed 2 years ago

dotoritos-kim commented 2 years ago

return new SqlError(msg, sql, fatal, info, sqlState, errno, additionalStack, addHeader); ^

SqlError: Cannot execute new commands: connection closed sql: ~~~ () { text: 'Cannot execute new commands: connection closed', sql: 'START TRANSACTION - parameters:[]', fatal: true, errno: 45013, sqlState: '08S01', code: 'ER_CMD_CONNECTION_CLOSED' }

If I connect to the DB for a long time and use a query, the above error occurs.

Create a pool like this:

masterPool = await mariaDB.createPool({
      host: masterServer.url,
      user: masterServer.user,
      password: masterServer.pw,
      connectionLimit: 10,
      database: masterServer.db,
      keepAliveDelay: 500,
    });

Test connection like this:

try {
      masterConn = await masterPool.getConnection();

      masterConn
        .ping()
        .then(() => {
          log.info(`mariaDB Master Connected`);
          masterConn.release();
        })
        .catch((err) => {
          log.error(`env: ${process.env.NODE_ENV}: mariaDB Error, ${err}`);
          masterConn.release();
        });
    } catch (err) {
      log.error(`env: ${process.env.NODE_ENV}: mariaDB Error, ${err}`);
    } finally {
      masterConn.release();
    }

Send query like below:

try{
  masterConn.beginTransaction();
  const findUser = await replicaConn.query(query string)
  await masterConn.commit();
  await masterConn.release();
  return findUser
}
catch(e){
  await masterConn.rollback();
  await masterConn.release();
  return e;
}

And no one was using it and only the application was on. This error occurs after approximately 4 hours. It was irregular.

I need help.

rusher commented 2 years ago

stackoverflow would be a better support for this kind of question. different remarks heres :

And to the problem : The error indicate that connection is closed. I don't know how you get that connection, but that probably is because connection reached wait_timeout timeout.