示例代码
try {
const oracledb = require('oracledb');
const express = require('express');
const app = express();
// Memorized Connections
var allDBSessions = {};
app.get('/', (req, res) => {
const dbConfig = {
user: 'vph_dev_ad_gss_tran',
password: 'Gsstpl100'
// ,connectString: '//IP:PORT/SERVICE_NAME' // Working as Expected
,connectString: '//IP:PORT/SERVICE_NAME?expire_time=1' // Minutes // Not Working
// ,connectString: '//IP:PORT/SERVICE_NAME?connect_timeout=15' // Seconds // Not Working
// Your connection string could be "mydbmachine.example.com/orclpdb1?expire_time=2" // Sample From Node Oracledb Site
};
async function ExecuteQuery() {
try {
// Getting DB Connections
let dbInstance;
if (allDBSessions.first_instance) {
// Assigning From Memorized Connections
console.log('Connection Available From the Memory');
console.log('Connection Created Time - ' + allDBSessions.first_instance.created_time);
dbInstance = allDBSessions.first_instance.db_connection;
console.log('Connection Assigned From Memory');
} else {
//Creating New DB Instance
console.log('Creating New Connection');
dbInstance = await oracledb.getConnection(dbConfig);
console.log('Connection Created Successfully');
// Memorizing the DB Instance
allDBSessions.first_instance = {
db_connection: dbInstance,
created_time: new Date().toLocaleString()
}
console.log('Connection Stored into Memory');
}
// Executing Query
var query = 'select 1 from dual';
console.log('Executing the Query - ' + query);
let queryResult = await dbInstance.execute(query);
console.log('queryResult', queryResult);
var finalResult = {
'Query Result': queryResult,
'Usedd DB Session Info': allDBSessions.first_instance
};
res.send(finalResult); // Sending Response
} catch (error) {
console.log(error, '----- Catch Error in ExecuteQuery()-----');
res.send(error);
}
}
ExecuteQuery();
})
app.listen(3005, () => {
console.log(`Oracle DB Sample With Expire Timeout`);
console.log(`server is listening on PORT 3005`);
})
} catch (error) {
console.log(error, '----- Catch Error -----');
}
尝试与 19c 的 Oracle Db 成功连接时出现上述示例抛出错误。
错误如下图
[错误:ORA-12514:TNS:侦听器当前不知道连接描述符中请求的服务] { errorNum:12514,偏移量:0 }
请指导我克服这个问题......
提前致谢...
快乐编码...