【问题标题】:How to connect to Oracle DB from Node.js with `createPool()`如何使用`createPool()` 从 Node.js 连接到 Oracle DB
【发布时间】:2021-10-21 21:54:34
【问题描述】:

我尝试将 oracle 数据库连接到我的项目。我使用 createpool 以便在将来为来自数据库的所有必要请求调用此函数。我的 config.js 文件:

const oracledb = require('oracledb')
oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT

const init = async function (query) {
  try {
    await oracledb.createPool({
      user: 'almat',
      password: 'almat789456123',
      connectString: '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xepdb1)))'
    })

    console.log('Connection pool started')

    await dostuff(query)
  } catch (err) {
    console.error('init() error: ' + err.message)
  } finally {
    // await closePoolAndExit()
  }
}

async function dostuff (query) {
  let connection
  try {
    connection = await oracledb.getConnection()
    const sql = query
    const binds = [1]
    const options = { outFormat: oracledb.OUT_FORMAT_OBJECT }
    const result = await connection.execute(sql, binds, options)
    console.log(result)
  } catch (err) {
    console.error(err)
  } finally {
    if (connection) {
      try {
        await connection.close()
      } catch (err) {
        console.error(err)
      }
    }
  }
}

async function closePoolAndExit () {
  console.log('\nTerminating')
  try {
    await oracledb.getPool().close(10)
    console.log('Pool closed')
    process.exit(0)
  } catch (err) {
    console.error(err.message)
    process.exit(1)
  }
}

process
  .once('SIGTERM', closePoolAndExit)
  .once('SIGINT', closePoolAndExit)

module.exports.init = init

我的 app.js 文件:

const express = require('express');
const config = require('./utils/config');

const app = express();

app.listen(3000, function () {
  console.log('Server running at port 3000')
})

app.set('view engine', 'ejs')
app.get('/', function (req, res) {
  return res.render('index')
})

app.get('/login', function (req, res) {
  return res.render('login')
})

app.get('/getCustomerName', function (req, res) {
  const query = 'SELECT firstname FROM customer WHERE :b = 1'
  const result = config.init(query)
  //console.log(typeof result)
  return res.send(result)
})

module.exports = app

当我请求 http://localhost:3000/getCustomerName 时,它​​返回空 json 文件,终端抛出此错误:NJS-047: poolAlias "default" not found in the connection pool cache

【问题讨论】:

    标签: node.js oracle connection node-oracledb


    【解决方案1】:

    createPool() 调用应在应用程序初始化期间运行一次,例如在您调用express() 的时候。来自createPool() doc

    此方法使用指定的用户名、密码和连接字符串创建连接池。池通常在应用程序初始化期间创建一次。

    init() 不应调用 doStuff()。创建池后,您的 Web 侦听器处理程序可以调用 dostuff()

    看基本的​​例子webapp.js

    另请参阅 Oracle 杂志系列 Build REST APIs for Node.js,其源代码为 here

    【讨论】:

    • 我看不懂逻辑,在例子中,我看到sql查询在一个异步函数中,并且在init()中调用了这个函数
    • 在 webapp.js 中,handleRequest 由 http 模块为每个 web 请求调用。稍微玩一下示例并通过调试器运行它们。
    猜你喜欢
    • 2016-06-17
    • 2023-04-04
    • 2018-07-18
    • 2016-03-11
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多