Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to release Oracle Database connection when Node.js exits

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

like image 722
Ratan Uday Kumar Avatar asked Sep 14 '26 20:09

Ratan Uday Kumar


2 Answers

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

like image 191
Ratan Uday Kumar Avatar answered Sep 16 '26 10:09

Ratan Uday Kumar


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.

like image 23
Dan McGhan Avatar answered Sep 16 '26 08:09

Dan McGhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!