Node.js Oracle DB connection pool. How to log how many connections are currently open

Viewed 22

I am new to Node JS world. I am using a Oracle DB in my application and creating a connection pool. Using this connection pool to query the DB. Application is working fine but For my testing, I need to log how many connections are open after every query.

How can I log how many connections are open.

Below is my code to create connection pool:

async function bbUserPool() {
  try {
     await oracledb.createPool({
        user          : config.user,
        password      : config.password,  
        connectString : config.connectString,
        poolAlias     : 'userpool',
        poolIncrement : 10,
        poolMax       : 20,
        poolMin       : 20
      });   
  } 
  catch (err) {
     console.error("Connection Pool Error:" + err.message)     
  }
}

How can I log how many connections are open.

1 Answers

The node-oracledb documention Connection Pool Monitoring has useful information.

Pool attributes connectionsInUse and connectionsOpen always provide basic information about an active pool:

const pool = await oracledb.createPool(...);

. . .

console.log(pool.connectionsOpen);   // how big the pool actually is
console.log(pool.connectionsInUse);  // how many of those connections are held by the application

This is from the pool implementation's point of view. The connectionsOpen value is how many 'pipes' (for want of a better word) have been established to the database. In your example, I would expect this to be 20 in normal operation. The connectionsInUse value is the count of getConnection() calls that have not had an explicit or implicit (at end of scope) corresponding connection.close() call.

Related