Search code examples
javascriptnode.jsoracle-databasenode-oracledb

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


Solution

  • 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