Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

294
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda