gpt4 book ai didi

node.js - 连接池永远不会用 node-oracledb 和 nodejs 关闭

转载 作者:行者123 更新时间:2023-12-04 08:41:23 30 4
gpt4 key购买 nike

我正在使用 Node Js 和 Oracle 制作一个小 API,为此使用 node-oracledb。我一直受到以下文章的指导:API with OracleDB and Node JS

项目文件包含以下内容:

/services/web-server.js

const http = require('http');
const morgan = require('morgan');
const express = require('express');
const webServerConfig = require('../config/web-server');

let httpServer;

function initialize(){
return new Promise((resolve, reject) => {
const app = express();
httpServer = http.createServer(app);

app.use(morgan('combined'));

app.get('/', (req, res) => {
res.end('Hello World');
});

httpServer.listen(webServerConfig.port, err => {
if(err){
reject(err);
return;
}

console.log(`Web server listening on localhost:${webServerConfig.port}`);

resolve();
});
});
}

module.exports.initialize = initialize;

function close(){
return new Promise((resolve, reject) => {
httpServer.close(err => {
if(err){
reject(err);
return;
}

resolve();
});
});
}

module.exports.close = close;

/services/database.js

const oracledb = require('oracledb');
const dbConfig = require('../config/database');

async function initialize(){
const pool = await oracledb.createPool(dbConfig.gtsmpPool);
}

module.exports.initialize = initialize;

async function close(){
await oracledb.getPool().close();
}

module.exports.close = close;

和/index.js

const webServer = require('./services/web-server');
const database = require('./services/database');
const dbConfig = require('./config/database');
const defaultThreadPoolSize = 4;

// Increase thread pool size by poolMax
process.env.UV_THREADPOOL_SIZE = dbConfig.gtsmpPool.poolMax + defaultThreadPoolSize;

async function startup(){
console.log('Starting application');

try{
console.log('Initializing database module');

await database.initialize();
}catch(err){
console.error(err);

process.exit(1); // Non-zero failure code
}


try{
console.log('Initializing web server module');

await webServer.initialize();
}catch(err){
console.error(err);

process.exit(1); // Non-zero failure code
}
}

startup();

async function shutdown(e){
let err = e;

console.log('Shutting down');

try{
console.log('Closing web server module');

await webServer.close();
}catch(e){
console.log('Encountered error', e);

err = err || e;
}

console.log('Exiting process');

try{
console.log('Closing database module');

await database.close();
}catch(err){
console.log('Encountered error', err);

err = err || e;
}

if(err){
process.exit(1); // Non-zero failure code
}else{
process.exit(0);
}
}

process.on('SIGTERM', () => {
console.log('Received SIGTERM');

shutdown();
});

process.on('SIGINT', () => {
console.log('Received SIGINT');

shutdown();
});

process.on('uncaughtException', err => {
console.log('Uncaught exception');
console.error(err);

shutdown(err);
});

/config/database.js

module.exports = {    
gtsmpPool: {
user: process.env.GTSMP_USER,
password: process.env.GTSMP_PASSWORD,
connectString: process.env.GTSMP_CONNECTIONSTRING,
poolMin: 10,
poolMax: 10,
poolIncrement: 0
}
};

连接数据库成功。问题是,当我想终止应用程序并关闭连接池时,等待语句 oracledb.getPool().close() 永远不会得到解决并且似乎卡住了。

如果我尝试强行完成,我会收到以下消息

Encountered error Error [ERR_SERVER_NOT_RUNNING]: Server is not running.
at Server.close (net.js:1604:12)
at Object.onceWrapper (events.js:416:28)
at Server.emit (events.js:310:20)
at emitCloseNT (net.js:1657:8)
at processTicksAndRejections (internal/process/task_queues.js:83:21) {
code: 'ERR_SERVER_NOT_RUNNING'

Encountered error Error: NJS-002: invalid pool

我在 docker 容器、Node v12.16.3 和 instantclient_18_5 上使用 Oracle 18c XE

知道如何解决吗?谢谢。

最佳答案

node-oracledb中有一些注释pool example ...

    // Get the pool from the pool cache and close it when no
// connections are in use, or force it closed after 10 seconds.
// If this hangs, you may need DISABLE_OOB=ON in a sqlnet.ora file.
// This setting should not be needed if both Oracle Client and Oracle
// Database are 19c (or later).
await oracledb.getPool().close(10);

关于node.js - 连接池永远不会用 node-oracledb 和 nodejs 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64548436/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com