I am using oracle database for my nodejs application.
When altering any table, i am getting error resource already in used.
This error getting because when terminating or when exiting the nodejs application the database connection is not getting released or closed.
I know how to release database connection
function doRelease() {
db.close(
function (err) {
if (err)
console.error(err.message);
});
}
But i dont know, how to call the above function on nodejs exit or node terminate??
So i want simple help
how to release db connection when exiting or terminating nodejs application
Any help will be appreciated
Similar Question on nodejs exit is in link
function doRelease() {
db.close(
function (err) {
if (err)
console.error(err.message);
});
console.log(`db released successfully`)
}
function killProcess() {
if (process.exitTimeoutId) {
return;
}
process.exitTimeoutId = setTimeout(process.exit, doRelease());
}
process.on('SIGTERM', killProcess);
process.on('SIGINT', killProcess);
process.on('uncaughtException', function (e) {
console.log('[uncaughtException] app will be terminated: ', e.stack);
killProcess();
});
console.log('Try to press CTRL+C or SIGNAL the process with PID: ', process.pid);
process.stdin.resume();
The above code have worked for me
The simple solution is to create a database module with methods like initialize and close. Then you need to call those methods at the right time. I like to tie them into various events in the main module.
Have a look at parts 1 and 2 in my series on Creating a REST API with Node.js and Oracle Database for ideas.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With