0

示例代码

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 }

请指导我克服这个问题......

提前致谢...

快乐编码...

4

1 回答 1

1

当node-oracledb 使用的 Oracle客户端库是 19c 或更高版本时,可以使用Easy Connect Plus语法。数据库版本无关紧要。请参阅文档中的架构图'//IP:PORT/SERVICE_NAME?expire_time=1'

通过运行示例应用程序version.js检查 node-oracledb 正在使用哪些客户端库。它将输出如下内容:

$ node version.js 
Run at: Thu Mar 18 2021 08:17:22 GMT+1100 (Australian Eastern Daylight Time)
Node.js version: v12.21.0 (darwin x64)
Node-oracledb version: 5.2.0-dev
Oracle Client library version: 19.8.0.0.0
Oracle Database version: 19.3.0.0.0

检查你Oracle Client library version是 19 岁或以后。

EXPIRE_TIME 更新:

  • 使用 18c 客户端库,它可以添加(EXPIRE_TIME=n)DESCRIPTION连接描述符的部分(在应用程序的完整连接描述符字符串中,或​​在 tnsnames.ora 文件中)。
  • 使用 19c 客户端库,它可以通过 Easy Connect 使用:host/service?expire_time=n.
  • 使用 21c 客户端库,它可以在客户端(即 Node.js 机器)sqlnet.ora 中使用。
于 2021-03-17T21:18:34.163 回答