I'm running a node.js app and randomly I'm getting the NJS-040 connection request timeout. Request exceeded queueTimeout of 60000 error. My code feels correct, and I can't seem to find any reason for the error to appear. Furthermore the error doesn't happen at any specific times - sometimes it works for over a day, sometimes it crashes after few hours. When it happens the only way to make it work is to manuallt stop and restart the app.
Heres my Oracle DB configuration:
const oracledb = require("oracledb");
exports.initOracle = async function init() {
try {
// Optional param to provide oracle bin for nodejs
oracledb.initOracleClient({ libDir: "C:\\instantclient_21_10"});
await oracledb.createPool({
user: process.env.ORACLE_USER,
password: process.env.ORACLE_PASSWORD,
connectString: process.env.ORACLE_STRING,
poolAlias: process.env.ORACLE_POOL,
poolMin: 1,
poolMax: 30,
poolIncrement: 1,
poolTimeout: 10
});
console.log("Oracle connection pool started");
} catch (err) {
console.error("init() error: " + err.message);
}
};
exports.closePool = async function () {
try {
const pool = await oracledb.getPool(process.env.ORACLE_POOL);
await pool.close();
} catch (err) {
console.log("Error occur while closing pool: ", process.env.ORACLE_POOL);
}
};
And thats the function that console shows its causing the problem:
//Search for patient in oracle
exports.getPatient = async (req, res, next) => {
let result;
let pesel = req.body.pesel;
// console.log('incoming pesel '+pesel)
let conn;
try {
conn = await oracledb.getConnection(process.env.ORACLE_POOL);
result = await conn.execute(
"SELECT id, imie, nazw, pesl FROM pacj WHERE pesl=:pesel",
{ pesel: pesel },
{ outFormat: oracledb.OBJECT }
);
if (result.rows.length !== 1) {
res.status(400).send({
message: "Patient not found in Oracle DB",
});
} else {
res.locals.patient = {
patientId: result.rows[0]["ID"],
firstname: result.rows[0]["IMIE"],
lastname: result.rows[0]["NAZW"],
pesel: result.rows[0]["PESL"],
};
next();
}
} catch (err) {
console.error(err);
res.status(500).send({
message: "Something went wrong while searching for patient query",
});
} finally {
if (conn) {
try {
await conn.close();
console.log("getPatient finally closed connection")
} catch (err) {
console.error(err);
}
}
}
};
According to console the source of error is is this line of code:
conn = await oracledb.getConnection(process.env.ORACLE_POOL);
However I don't see anything wrong here. Any suggestions how to fix the problem?
I've tried moving the await conn.close(); function from finally to inside the if/else loop, but that didn't do anything. I've also added the console.log("getPatient finally closed connection") inside the funally clause to see if it's working - and it seems to be closing the connection correctly.