具有 node.js 的 Oracle 数据库连接池
Oracle Database Connection Pool with node.js
我是 Node.js 的新手。我正在尝试使用多个数据库创建连接池。我已经使用下面提到的代码成功创建了连接池(我认为)。我知道为了执行查询操作我必须在 "Connection pool DBX Success" 处做一些事情,但我似乎无法弄清楚该怎么做才能对所需的池执行查询说 crm1.execute 或crm2.execute。我可以在这里做什么来实现这一目标。我能想到的唯一方法是分别为每个数据库编写执行函数,我知道这是错误的,我必须使用 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 documentation on pooling and examples。先研究那些。
然后您可能会发现,给每个池一个 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);
我是 Node.js 的新手。我正在尝试使用多个数据库创建连接池。我已经使用下面提到的代码成功创建了连接池(我认为)。我知道为了执行查询操作我必须在 "Connection pool DBX Success" 处做一些事情,但我似乎无法弄清楚该怎么做才能对所需的池执行查询说 crm1.execute 或crm2.execute。我可以在这里做什么来实现这一目标。我能想到的唯一方法是分别为每个数据库编写执行函数,我知道这是错误的,我必须使用 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 documentation on pooling and examples。先研究那些。
然后您可能会发现,给每个池一个 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);