Muhammad Abdullah
Muhammad Abdullah

Reputation: 322

Oracle Database Connection Pool with node.js

I am new to Node.js. I am trying to make connection pools with multiple databases. I have successfully made connection pools (i think) with below mentioned code. I know in order to execute query operations i have to do something at "Connection pool DBX Success", but i can't seem to figure out what to do so that i am able to execute queries on desired pool say crm1.execute or crm2.execute. What can i do here to achieve this. The only way i can think of is to write execute functions for each database separately which i know is wrong and i have to work with 15 databases so it isn't possible to write functions for all 15 databases separately.

const config = require("../config/config");
const oracledb = require("oracledb");

 crm1 = config.crm1;
 crm2 = config.crm2;

const crm1pool = oracledb.createPool ({
    user: crm1.user,
    password: crm1.password,
    connectString: crm1.connectString,
    poolMin: 1,
    poolMax: 10,
    poolTimeout: 300
}, (error,pool)=>{
    if (error){
        console.log(error);
    }
    console.log("Connection Pool DB1 success")
});

const crm2pool = oracledb.createPool ({
    user: crm2.user,
    password: crm2.password,
    connectString: crm2.connectString,
    poolMin: 1,
    poolMax: 10,
    poolTimeout: 300
}, (error,pool)=>{
    if (error){
        console.log(error);
    }
    console.log("Connection Pool DB2 success")
});

Upvotes: 1

Views: 8689

Answers (1)

Christopher Jones
Christopher Jones

Reputation: 10586

There is a lot of node-oracledb documentation on pooling and examples. Study those first.

Then you might find that giving each pool a poolAlias will let you easily choose which to use:

await oracledb.createPool({
  user: 'hr',
  password: myhrpw,  // myhrpw contains the hr schema password
  connectString: 'localhost/XEPDB1',
  poolAlias: 'hrpool'
});

await oracledb.createPool({
  user: 'sh',
  password: myshpw,  // myshpw contains the sh schema password
  connectString: 'otherhost/OTHERDB',
  poolAlias: 'shpool'
});

const connection = await oracledb.getConnection('hrpool');

const result = await connection.execute(
      `SELECT manager_id, department_id, department_name
       FROM departments
       WHERE manager_id = :id`,
      [103],  // bind value for :id
    );
    console.log(result.rows);

Upvotes: 4

Related Questions