Search code examples
node.jsnode-oracledb

Node oracledb connection pool not closing before node process termination


Node application when terminating is not closing the db sessions. I am currently using node-cleanup library, but when the pool connections are above a certain number the pool.close() function will take some time to resolve so the node terminates without closing the pool and sessions remain inactive in the db.Have already set proper time outs in oracle DB so that inactive sessions are sniped and cleaned up. But wanted to know is there some way I can block the node process exit so that the pool.close() is resolved.

nodeCleanup(function(exitCode, signal) {
    console.log('Node server exiting with signal : ' + signal);
    console.log('Node server exiting with exitCode : ' + exitCode);
    db.getPool().close();
});

But if I use the below service and then exit the node all db sessions are closed properly.

app.get('/closePool', function(req, res, next) {
    db.getPool().close(function(err) {
        if (err)
            next(err);
        res.send('Pool closed');
    });
})

Solution

  • Have you tried to use the beforeExit event? https://nodejs.org/api/process.html#process_event_beforeexit