Node-oracledb: Question why getting Error: NJS-046: poolAlias "xxxPool" already exists in the connection pool cache

Created on 1 Oct 2018  路  4Comments  路  Source: oracle/node-oracledb

Answer the following questions:

  1. What is your Node.js version? Is it 64-bit or 32-bit? Run version.js from https://github.com/oracle/node-oracledb/blob/master/examples/version.js

Node.js version: 6.9.1, 64-bit

  1. What is your node-oracledb version?

Node-oracledb version: 2.3.0

  1. What exact command caused the problem (e.g. what command did you try to install with)? Who were you logged in as?

npm run start

  1. What error(s) you are seeing?
    screen shot 2018-10-01 at 3 14 13 pm

  2. What OS (and version) is Node.js executing on?

MacOS version: 10.13.6

  1. What is your Oracle client (e.g. Instant Client) version? Is it 64-bit or 32-bit? How was it installed? Where is it installed?

screen shot 2018-10-01 at 3 04 34 pm

  1. What is your Oracle Database version?

I am not sure, I just tried to use this guide: https://oracle.github.io/node-oracledb/INSTALL.html#instosx and although the app runs and I can debug, I get the error above.

  1. What is the PATH environment variable (on Windows) or LD_LIBRARY_PATH (on Linux) set to? On macOS, what is in ~/lib?

screen shot 2018-10-01 at 3 00 18 pm

  1. What Oracle environment variables did you set? How exactly did you set them?

I am not sure exactly

  1. Do you have a small, single Node.js script that immediately runs to show us the problem?
    Unfortunately no but here is some code:

Top of my oracle.js file with create pool function (code formatting not working here for me so using screenshot):
screen shot 2018-10-01 at 3 39 21 pm

Connect:
` connect() {

var connAttrs = (this.creds.poolAlias) ? this.creds.poolAlias : this.creds;

return oracledb.getConnection(connAttrs)
  .catch((err) => {

    debug('connect failed');
    if (_.isUndefined(this.creds.poolAlias)) {
      throw new Error(err);
    }
    else {
      return this._catchPoolErr(err, this.creds.poolAlias)
        .then(() => {
          this.connect();
        });
    }
  });

},`

Try to createPool if does not exist:
` _catchPoolErr(err, poolAlias) {

// connection pool not found. create and reconnect  
if (err.toString().match('NJS-047')) {
  debug(`connection pool ${poolAlias} not found. Will request creation`);
  return this.createPool(poolAlias);
}
else {
  throw new Error(err);
}

}`

question

All 4 comments

@rstor1 The way you've written the code, you don't create the pool until a connection request is made. But what happens if two connection requests are made at the same time? That's probably what's leading to the error.

I tend to avoid this alltogether by creating the pool first, just get that out of the way, and then open up the other parts of the app that would service requests. I show how to do this with REST APIs here:
https://jsao.io/2018/03/creating-a-rest-api-with-node-js-and-oracle-database/

It's off topic, but I'm curious, why do you throw a new error

throw new Error(err);

instead of just throwing the existing error?

throw err;

@rstor1 there are also some basic pool examples in the webapp*.js files, e.g. webappawait.js.

@dmcghan thanks for the feeback, will look into this! I guess I should state that I did not write this code, I am helping maintaining and debug an existing service so I am not sure why the decision on throwing the new error. That could be another possible improvement though thanks!

@cjbj thanks for pointing me to the right examples!

Was this page helpful?
0 / 5 - 0 ratings