Error: NJS-047: poolAlias "default" not found in the Connection pool cache

Asked

Viewed 649 times

2

I’m trying to make a connection using Connection Pool to build a API Nodejs consuming data from Oracledb, I followed the following documentation. However, when trying to make the connection I get the following error:

Error: NJS-047: poolAlias "default" not found in the Connection pool cache

I tried to put a poolAlias manually in the Oracle connection configuration but nothing resolves.

The connection configuration class, database js., is as follows:

module.exports = {
 hrPool: {
   user: "meuUsuario",
   password: "minhaSenha",
   connectString: "localhost/XE"
 }
};

The class that is called to connect and execute a query is as follows:

const oracledb = require('oracledb');
const dbConfig = require('../config/database.js');

async function initialize() {
  const pool = await oracledb.createPool(dbConfig.hrPool);
}

async function close() {
    await oracledb.getPool().close();
}

function simpleExecute(statement, binds = [], opts = {}) {
    return new Promise(async (resolve, reject) => {
      let conn;

      opts.outFormat = oracledb.OBJECT;
      opts.autoCommit = true;

     try {
       conn = await oracledb.getConnection();

       const result = await conn.execute(statement, binds, opts);

       resolve(result);
     } catch (err) {
       reject(err);
     } finally {
       if (conn) { // conn assignment worked, need to close
         try {
           await conn.close();
         } catch (err) {
           console.log(err);
         }
       }
     }
   });
 }

module.exports.simpleExecute = simpleExecute;
module.exports.close = close;
module.exports.initialize = initialize;

I could not identify the problem, since I use async in asynchronous calls and the documentation follows the same model...

Only as additional information, the index js. starts the application as follows:

  async function startup(){
  console.log('Starting application');

  try {
    console.log('Initializing web server module');

    await webServer.initialize();
  } catch (err) {
    console.error(err);

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

startup();

And the web-server.js that is called in function startup() has the method initialize() as follows:

const http = require('http');
const express = require('express');
const morgan = require('morgan');
const webServerConfig = require('../config/web-server.js');
const database = require('./database.js');

let httpServer;

function initialize() {
  return new Promise((resolve, reject) => {
    const app = express();
    httpServer = http.createServer(app);

    // Combines logging info from request and response
    app.use(morgan('combined'));

    app.get('/', async (req, res) => {
      const result = await database.simpleExecute('select user, systimestamp from dual');
      const user = result.rows[0].USER;
      const date = result.rows[0].SYSTIMESTAMP;

      res.end(`DB user: ${user}\nDate: ${date}`);
    });

    httpServer.listen(webServerConfig.port)
      .on('listening', () => {
        console.log(`Web server listening on localhost:${webServerConfig.port}`);

        resolve();
      })
      .on('error', err => {
        reject(err);
      });
  });
}

The method initialize executes the database.simpleExecute('select user, systimestamp from dual'); that ends up giving the connection problem.

1 answer

2


After several attempts, I arrived at a solution:

Checking the internal code of the Node oracledb, I noticed that the getConnection method receives some connection attributes as parameter:

function getConnection(connectionAttributes: ConnectionAttributes): Promise<Connection>;

Therefore, just insert the pool configuration as a parameter for the method to work, before it was like this:

conn = await oracledb.getConnection();

Stayed like this:

conn = await oracledb.getConnection(dbConfig.hrPool);

May this solution help many, since not even the documentation presents a solution to the problem.

  • Thank you very Much for this, my getConnection method was Working Perfectly until it stopped. Searched Everywhere for an Answer and Yours helped

  • @Sirajkakeh really Glad to read this!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.