Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

295
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda