Home > Software engineering >  Unable to connect two databases in Express
Unable to connect two databases in Express

Time:09-16

I´m having a issue when using 2 DB's at same time (both oracle). My startup function only execute first connection try catch block, but show console.log connection message of second try catch block, but the connection is never made.

Here's my startup function:

async function startup() {
  try {
    console.log('Initializing SEI database module...');
    await seiDatabase.initialize();
  } catch (err) {
    console.error(err);
    process.exit(1); // Non-zero failure code
  }
  console.log('Starting SEI application...');

  try {
    console.log('Initializing SIP database module...');
    await sipDatabase.initialize();
  } catch (err) {
    console.error(err);
    process.exit(1); // Non-zero failure code
  }
  console.log('Starting SIP application...');

  try {
    console.log('Initializing web server module...');
    await webServer.initialize();
  } catch (err) {
    console.error(err);

    process.exit(1); // Non-zero failure code
  }
}
startup();

I´m using the same webServer.initialize for both... When I try to use my SIP route to get data, this error is returned: message "ORA-00942: table or view does not exist", my route is trying to get data from SIP DB, but only SEI DB is connected... I'm doing the multiple connection in the wrong way?

Im using Node, Express and oracledb ^5.4.0"... not using any TypeORM.

CodePudding user response:

If you are using two pools, make sure to assign and then use a pool alias name (or pass around the pool object). Check the doc Connection Pool Cache.

For example, two pools could be created like:

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

await oracledb.createPool({
  user: 'sh',
  password: myshpw,
  connectString: 'localhost/XEPDB1',
  poolAlias: 'shpool'
});

Then you would get a connection from the desired pool by specifying its alias. For example:

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

. . . // Use connection from the HR pool

await connection.close();
  • Related