You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js连接Oracle数据库时pool.getConnection触发NJS-510超时错误的问题求助

Node.js连接Oracle数据库时pool.getConnection触发NJS-510超时错误的问题求助

大家好,我第一次在Stack Overflow提问,要是表述不清楚或者格式有问题还请见谅!最近我在开发Node.js + Oracle数据库的路由时遇到了棘手的问题:明明已经通过await db.connectToDatabase()成功连接到数据库并拿到了连接池,但调用pool.getConnection时直接触发了NJS-510超时错误,程序完全没进入这个方法的回调逻辑,想请大家帮忙排查下问题所在。

问题详细现象

  • 执行await db.connectToDatabase()时能成功创建Oracle连接池,控制台能打印出连接成功的日志
  • 但走到pool.getConnection这一步时,程序直接报错NJS-510,根本没进入这个方法的回调函数
  • SSH连接看起来是正常的,没有抛出连接失败的错误

我的路由代码(route.js)

const express = require('express');
const route = express.Router();
const db = require("../db")

// Oracle connect pool
let pool;

// 注:原代码箭头函数写成了`async (req, res) ={`,我本地已经修正为正确的`=>`
route.get("/chooseUser", async (req, res) => {
  let sql = "SELECT * FROM User WHERE email = ?";
  let params = ["john.doe@example.com"];

  console.log("getUser", req.query);
  db.connectSSH();

  try {
    // Ensure the database is connected
    await db.connectToDatabase();
    // Get the pool
    pool = db.getPool();
  } catch (err) {
    // 注:原代码try块未写闭合的`}`,我本地已经修正
    console.error('Database error:', err);
  }

  console.log("getUser:", req.query);

  // 错误发生在这里:程序完全没进入这个方法
  pool.getConnection(function(err, connection) {
    if (err) throw err;

    connection.query(sql, params, function(queryError, results, fields) {
      db.disconnectFromDatabase();
      console.log(1);

      if(queryError) {
        console.error('Query error:', queryError);
        return res.status(500).render('error', { error: queryError });
      }

      console.log(results);
      // 原代码标注这里渲染也有问题,但当前核心问题是getConnection的超时
      res.render('userChoose', { users: results });
    });
  });
})

module.exports = route; // expose

我的数据库配置文件(db.js)

(注:config里的config.mysql只是我没改config.js里的变量名,实际配置的是Oracle的参数,这部分没问题)

const { Client } = require('ssh2');
const oracledb = require('oracledb');
const configs = require('./config')

const con = new Client();

console.log('initializing:', configs);
console.log('initializing:', configs.sshConfig.port);

function connectSSH() {
  const con = new Client();
  con.on('ready', () => {
    console.log('Connection :: ready');
    con.shell(function(err, stream) {
      if (err) {
        console.log('err over ')
      } else {
        connectToDatabase();
        var buf = '';
        var bData = false;
        stream.on('data', function(data) {
          if (bData == false) {
            bData = true;
            stream.write('su\n');
          }
          if (data.indexOf('Password:') > -1) {
            stream.write('Pxxxxxxxx')
          }
          console.log(data.toString())
          if (data.indexOf('# ') > -1) {
            console.log('successful')
            stream.end()
          }
        }).on('close', function() {
          console.log('success over ')
        }).stderr.on('data', function(data) {
          console.log('stderr: ' + data);
        });
      }
    });
  });

  con.on('error', (err) => {
    console.log('Connection :: error :: ' + err);
  });
  con.on('end', () => {
    console.log('Connection :: end');
  });
  con.on('close', (had_error) => {
    console.log('Connection :: close');
  });

  // Start the connection
  con.connect(configs.sshConfig);
  return con; // Returning the connection object might be useful
}

con.connect({
  host: configs.sshConfig.host,
  port: configs.sshConfig.port,
  username: configs.sshConfig.username,
  password: configs.sshConfig.password
})

let pool;

async function connectToDatabase() {
  try {
    const dbConfig = {
      user: configs.mysql.user,
      password: configs.mysql.password,
      connectString: 'myConnectString',
    };
    pool = await oracledb.createPool(dbConfig);
    console.log('Connected to the remote Oracle database successfully.');
  } catch (err) {
    console.error('Error connecting to the remote Oracle database:', err);
  } finally {
  }
}

async function disconnectFromDatabase() {
  if (pool) {
    try {
      await pool.close();
      console.log('Database connection closed successfully.');
    } catch (err) {
      console.error('Error closing the database connection:', err);
      throw err; // Optionally re-throw the error for the caller to handle
    }
  }
  if (con) {
    con.end(); // Assuming con.end() is the correct way to close your SSH connection
    console.log('SSH tunnel connection closed');
  }
}

function closeSSH() {
  if (con) {
    con.end(); // Close the SSH connection
    console.log('SSH tunnel connection closed');
  }
}

module.exports = {
  connectToDatabase,
  getPool: () => pool, // Expose a getter for the pool
  disconnectFromDatabase,
  connectSSH,
  closeSSH
};

我已经排查了SSH连接、Oracle配置(connectString、用户名密码)这些点,看起来都没问题,但就是卡在pool.getConnection这一步报NJS-510超时,希望有经验的朋友能帮我找找问题根源,谢谢大家!


备注:内容来源于stack exchange,提问作者blu2eh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 07:19:30