【问题标题】:Node.js oracledb module -- TypeError: Cannot read property 'close' of undefinedNode.js oracledb 模块——类型错误:无法读取未定义的属性“关闭”
【发布时间】:2021-07-17 23:40:54
【问题描述】:

我正在尝试使用 Oracledb npm for Nodejs 运行简单的 SQL 语句。这在过去没有问题,但突然我在不同的环境中遇到了这个错误。可能是因为别名?我不确定。

导致问题的线路的目标是在连接完成或失败后关闭连接。我直接在 Oracle 的文档中找到了逻辑:http://oracle.github.io/node-oracledb/doc/api.html#connectionclose

非常感谢任何帮助!

const oracledb = require('oracledb');
const async = require('async');
const {getAwsSecret} = require('./awssecret');
var alias = "";

async function initialize() {
  // Set Oracle Pool Settings
  let hrPool = {
    poolMin: 10,
    poolMax: 10,
    poolIncrement: 0
  }

  var secret = await getAwsSecret('MAIN');
  const secretJSON = JSON.parse(secret);
  hrPool.user = secretJSON[process.env.CURENV + 'username'];
  hrPool.password = secretJSON[process.env.CURENV + 'password'];
  hrPool.connectString = secretJSON[process.env.CURENV + 'host'] + '/' + secretJSON[process.env.CURENV + 'dbname'];
  hrPool.poolAlias = secretJSON.alias;
  alias = secretJSON.alias;

  try { 
  await oracledb.createPool(hrPool);
  } catch (err) {
    console.log(err);
  }
}

module.exports.initialize = initialize;

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

module.exports.close = close;

async function simpleExecute(statement, binds = [],clientdetails = [], opts = {}) {
    let conn;
    opts.outFormat = oracledb.OBJECT;
    opts.autoCommit = true;
    try {
      // Get Connection
      conn = await oracledb.getConnection(alias);
      // Run Query
      const result = await conn.execute(statement, binds, opts);
    ///////////// POST EXECUTION HANDLING /////////////
    if (conn) { // conn assignment worked, need to close
      try {
        // Close Connection
        await conn.close();
        // Return Result
        return result;
      } catch (err) {
        console.log(err);
      }
    }
    ///////////// POST EXECUTION HANDLING /////////////
    } catch (err) {
      await conn.close();

    } 
  }

module.exports.simpleExecute = simpleExecute;

错误:

TypeError: Cannot read property 'close' of undefined 2021-04-23T14:36:51.269-04:00  at 
Object.simpleExecute (/usr/src/app/services/database.js:59:18)

【问题讨论】:

    标签: node.js oracle node-oracledb


    【解决方案1】:

    如果获取连接时出错,您的 catch 块会调用 conn.close() 而没有有效的 conn。这给出了您看到的错误。这可能不是唯一的原因,但整理代码会有所帮助。

    您的conn.close() 可能在finally() 块中只有一次。查看example.js 之类的示例,例如喜欢:

      let connection;
    
      try {
        connection = await oracledb.getConnection(dbConfig);
    
        result = await connection.execute(sql, binds, options);
        console.dir(result, { depth: null });
    
      } catch (err) {
        console.error(err);
      } finally {
        if (connection) {
          try {
            await connection.close();
          } catch (err) {
            console.error(err);
          }
        }
      }
    

    其他一些提示:

    • 如果你的代码要连续执行几个语句,那么不要在每个语句周围都使用getConnection()/close()。这会影响池的可扩展性,因为共享池资源存在一些锁定,并且会在整个步骤中添加额外的代码。
    • 避免总是提交。如果不必要地这样做,这可能会破坏事务一致性并增加开销。
    • pool.close() 调用中使用显式的drainTime 以确保它已关闭,请参阅webapp.js

    【讨论】:

    • 感谢您提供的信息——根据您上面的信息,您实际在哪里使用 pool.close()?看起来它只是使用连接的标准 connection.close() 属性。我担心我可能会混淆这两者并给自己带来更多问题。我现在不相信我的代码实际上调用了关闭池(相对于连接)。
    • 另一个问题——在上面的例子中,它实际返回的结果在哪里?我正在使用这些 oracle 调用将数据返回到我的前端。 (我没有看到返回结果)。
    • 您可以在应用关闭时关闭池。对于像webapp.js 这样的网络应用程序,这是进程停止的时候。关于返回数据,将console.dir行替换为return。
    • 再次感谢,最后如果您不介意我还有一个问题。如果我使用池别名会改变什么吗?我将来可能会有一个用例,其中别名将是特定于客户端的,以便根据登录使用 Oracle VPD。
    • Pool aliases 不会影响任何东西。如果每个 Node.js 进程只有一个池,那么您实际上不需要更改默认池别名。对于 VPD,请考虑使用 connection.clientID 而不是拥有多个池。
    猜你喜欢
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2015-04-23
    • 1970-01-01
    • 2021-04-15
    • 2021-12-03
    相关资源
    最近更新 更多