Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

292
Views
Where to initOracleClient in REST MVC Architecture

I have Express Routing set up with multiple routes, each using a different Oracle connection. I have to call initOracleClient prior to getConnection, however I get an error (Error: NJS-077: Oracle Client library has already been initialized) when I try to initOracleClient in both routes. I've tried moving the initOracleClient to different locations in the structure; both at the app level and route level. Where in a REST MVC structure do you initialize the client?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

A REST MVC application typically has some supporting infrastructure. That is to say, MVC is not a complete blueprint on how to structure the entirety of your program's code - only a general rule of thumb of how to assign certain responsibilities.

The library you're using needs initialization, and apparently this code should execute only once. There are several ways to go about it:

  1. Initialize the client once before starting up the express server, and then pass in the ready-to-use client for use by route handlers. This may be the easiest to use, but must necessarily delay the .listen() call - so the time until your application starts responding to HTTP may be longer.
  2. Use a pattern known as the Singleton to allow route handlers to initialize the client, but only execute the initialization once under the hood. Depending on how exactly the library is initialized (does it return a Promise? does it use a callback?), this may require some careful design - for example, you may need to store and return a Promise instance, so multiple consumers will be calling .then() on the same Promise.
about 4 years ago · Juan Pablo Isaza Report

0

I implemented the Singleton pattern as suggested:

import oracledb from 'oracledb';

class PrivateOraInitSingleton {
  constructor() {
    try {
      oracledb.initOracleClient({libDir: '/usr/local/lib/instantclient_19_8'});
    } catch (err) {
      console.error(err);
      process.exit(1);
    }
  }
}

class OraInitSingleton {
    constructor() {
        throw new Error('Use OraInitSingleton.getInstance()');
    }
    static getInstance() {
        if (!OraInitSingleton.instance) {
            OraInitSingleton.instance = new PrivateOraInitSingleton();
        }
        return OraInitSingleton.instance;
    }
}

export default OraInitSingleton;

Usage:

const object = OraInitSingleton.getInstance();

try {
  connectionPromise = oracledb.getConnection({
    user : process.env.DB_USER,
    password : process.env.DB_PASSWORD,
    connectString : process.env.CONNECT_STRING
  });
} catch (err) {
  console.error(err);
  process.exit(1);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!