简体   繁体   中英

Node Oracle DB using 'node-oracledb'

So I'm trying to add Oracle DB support in my Node App. I'm using node-oracledb to do this.

As I am also using MSSQL in my app already, I want to implement a 'similar' workflow to use Oracle.

Right now I use a class that implements methods using promises, like this:

class db {

    constructor(){
        this._pool = null;
    }

    get_pool(){
       //for MSSQL
       if (!this._pool) {
           this._pool = new mssql.ConnectionPool(sqlDbOptions);
       }

       //connect pool
       if (!this._pool.connected){
           return this._pool.connect();
       }
       else{
           return new Promise((resolve, reject) => {
               resolve(this._pool);
           })
       }
    }

    insert(sql){
        return this.get_pool().then((pool) => {...
        ....
        ....
    }
}

As you can see, calling the insert method gets returns the insert promise after getting the pool from get_pool() (which checks if the pool is connected, and connects it if it isn't).

I tried using the same logic for oracle, from what I'm reading in their docs for promises, using something like this in the get_pool():

if (!this._pool){
    this._pool = new oracle.createPool(oracleDbOptions);
}

if (this._pool.connectionsOpen == 0){
    return this._pool.getConnection();
}
else{
    return new Promise((resolve, reject) => {
        resolve(this._pool);
    })
}

This isn't working, as for some reason the oracle.createPool(options) also returns a promise, which I noticed takes a lot to resolve (no idea why, as it isn't connecting).

Any idea how to achieve the same behavior as the one I used for MSSQL here?

Thanks!

Have a look at my post on " Database Basics ", part of a series on creating a REST API with Node.js and Oracle Database.

I generally advocate creating and shutting down the pool as part of your startup and shutdown code. Then I use a function like this to simplify simple executes that don't need a transaction:

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);
        }
      }
    }
  });
}

Note that with node-oracledb, when you call oracledb.createPool, the connections to the database are created at that time (that's why it's an async operation).

Also, we have a " Connection Pool Cache ", which allows you to assign aliases to pools when you create them and retrieve them with oracledb.getPool(), which is a synchronous method.

In the example above, because the call to oracledb.getConnection() doesn't have any parameters passed to it, it's assumed that the connection should come from the "default" pool, which is the first pool that's created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM