mysql pool connection through ssh in nodejs

200 Views Asked by At

when I was creating mysql pool with mysql.createPool({}) using mysql2 package in nodejs with 10 connection limit I was able to use 10 connections simultaneously without releasing any connection. but after creating mysql.createPool({}) through ssh using ssh2 package. I was able to use 1 connection at a time even after setting connection limit to 10 in pool config object and only after releasing that connection, pool.getConnection() returns a mysql connection. Why is that?

Pool Creation through ssh Code

function dbConnect (){
  return new Promise ((resolve,reject) => {
    if(process.env.NODE_ENV=='PRODUCTION'){

      pool = mysql.createPool({
        ...dbConfig,
        connectionLimit: 10,
        // timezone:'UTC',
        multipleStatements: true,
      });
      resolve('success');
    
    }else if(process.env.NODE_ENV=='DEVELOPMENT'){
    
      let sshClient = new Client();
      sshClient.on('ready', () => {
    
        sshClient.forwardOut(
          forwardConfig.srcHost,
          forwardConfig.srcPort,
          forwardConfig.dstHost,
          forwardConfig.dstPort,
            (err, stream) => {
            if (err)  reject('error');
            console.log("stream",stream);
           pool = mysql.createPool({
            ...dbConfig,
            connectionLimit: 10,
            // timezone:'UTC',
            multipleStatements: true,
            stream
          });

          resolve('success');     
        });
    }).connect({
      host: process.env.DB_HOST,
      username: 'root',
      port:22,
      // password:'GJe6ecLNzNR(g!P4',
      privateKey: require('fs').readFileSync(process.env.SSHPRRIVATEKEYPATH),
    });
    }
  })
}

Connection Code


function getConnection() {
  return new Promise(function (res, rej) {
    pool
      .getConnection()
      .then((conn) => {
        res(conn);
      })
      .catch((err) => {
        rej(err);
      });
  });
}

function getTransaction() {
  return new Promise((res, rej) => {
    console.log("get Transaction",pool);
    pool
      .getConnection()
      .then(async (conn) => {
        try {
          await conn.beginTransaction();
          console.log("get Transaction");
          res(conn);
        } catch (err) {
          rej(err);
        }
      })
      .catch((err) => {
        rej(err);
      });
  });
}
0

There are 0 best solutions below