Reputation: 774
I am trying to run simple SQL statements using Oracledb npm for Nodejs. This has worked in the past with no issue but suddenly I am getting this error in a different environment. Possibly due to aliases? I am not sure.
The goal of the line causing the issue is to close out the connection once it completes or fails. I found the logic directly in the docs from Oracle found here: http://oracle.github.io/node-oracledb/doc/api.html#connectionclose
Really would appreciate any help!
const oracledb = require('oracledb');
const async = require('async');
const {getAwsSecret} = require('./awssecret');
var alias = "";
async function initialize() {
// Set Oracle Pool Settings
let hrPool = {
poolMin: 10,
poolMax: 10,
poolIncrement: 0
}
var secret = await getAwsSecret('MAIN');
const secretJSON = JSON.parse(secret);
hrPool.user = secretJSON[process.env.CURENV + 'username'];
hrPool.password = secretJSON[process.env.CURENV + 'password'];
hrPool.connectString = secretJSON[process.env.CURENV + 'host'] + '/' + secretJSON[process.env.CURENV + 'dbname'];
hrPool.poolAlias = secretJSON.alias;
alias = secretJSON.alias;
try {
await oracledb.createPool(hrPool);
} catch (err) {
console.log(err);
}
}
module.exports.initialize = initialize;
async function close() {
await oracledb.getPool().close();
}
module.exports.close = close;
async function simpleExecute(statement, binds = [],clientdetails = [], opts = {}) {
let conn;
opts.outFormat = oracledb.OBJECT;
opts.autoCommit = true;
try {
// Get Connection
conn = await oracledb.getConnection(alias);
// Run Query
const result = await conn.execute(statement, binds, opts);
///////////// POST EXECUTION HANDLING /////////////
if (conn) { // conn assignment worked, need to close
try {
// Close Connection
await conn.close();
// Return Result
return result;
} catch (err) {
console.log(err);
}
}
///////////// POST EXECUTION HANDLING /////////////
} catch (err) {
await conn.close();
}
}
module.exports.simpleExecute = simpleExecute;
Error:
TypeError: Cannot read property 'close' of undefined 2021-04-23T14:36:51.269-04:00 at
Object.simpleExecute (/usr/src/app/services/database.js:59:18)
Upvotes: 1
Views: 1137
Reputation: 10496
If there is an error getting the connection, your catch block calls conn.close()
without having a valid conn
. This gives the error you see. It may not be the only cause, but tidying the code will help.
Your conn.close()
could be just once in a finally()
block. Look at examples like example.js e.g. like:
let connection;
try {
connection = await oracledb.getConnection(dbConfig);
result = await connection.execute(sql, binds, options);
console.dir(result, { depth: null });
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
Some other tips:
getConnection()
/close()
around each. This affects pool scalability because there is some latching on the shared pool resource, and it adds extra code to the whole step.drainTime
in the pool.close()
call to ensure it is closed, see webapp.js.Upvotes: 2