繁体   English   中英

Oracle 数据库连接池与 node.js

[英]Oracle Database Connection Pool with node.js

我是 Node.js 的新手。 我正在尝试使用多个数据库建立连接池。 我已经用下面提到的代码成功地建立了连接池(我认为)。 我知道为了执行查询操作,我必须在“连接池 DBX 成功”处做一些事情,但我似乎不知道该怎么做才能在所需的池上执行查询,比如 crm1.execute 或 crm2 。执行。 我可以在这里做些什么来实现这一目标。 我能想到的唯一方法是分别为每个数据库编写执行函数,我知道这是错误的,我必须使用 15 个数据库,因此不可能分别为所有 15 个数据库编写函数。

 const config = require("../config/config"); const oracledb = require("oracledb"); crm1 = config.crm1; crm2 = config.crm2; const crm1pool = oracledb.createPool ({ user: crm1.user, password: crm1.password, connectString: crm1.connectString, poolMin: 1, poolMax: 10, poolTimeout: 300 }, (error,pool)=>{ if (error){ console.log(error); } console.log("Connection Pool DB1 success") }); const crm2pool = oracledb.createPool ({ user: crm2.user, password: crm2.password, connectString: crm2.connectString, poolMin: 1, poolMax: 10, poolTimeout: 300 }, (error,pool)=>{ if (error){ console.log(error); } console.log("Connection Pool DB2 success") });

有很多关于池和示例node-oracledb 文档 先研究那些。

然后你可能会发现给每个池一个poolAlias会让你很容易选择使用哪个:

await oracledb.createPool({
  user: 'hr',
  password: myhrpw,  // myhrpw contains the hr schema password
  connectString: 'localhost/XEPDB1',
  poolAlias: 'hrpool'
});

await oracledb.createPool({
  user: 'sh',
  password: myshpw,  // myshpw contains the sh schema password
  connectString: 'otherhost/OTHERDB',
  poolAlias: 'shpool'
});

const connection = await oracledb.getConnection('hrpool');

const result = await connection.execute(
      `SELECT manager_id, department_id, department_name
       FROM departments
       WHERE manager_id = :id`,
      [103],  // bind value for :id
    );
    console.log(result.rows);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM