3

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

Christopher Jones
  • 9,449
  • 3
  • 24
  • 48
Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
  • https://stackoverflow.com/questions/14031763/doing-a-cleanup-action-just-before-node-js-exits that link has an example of how to handle various close/exit signals that the process can get and how to call a function before handling them. You could close the DB connection there – Jayce444 Jun 05 '18 at 05:29

4 Answers4

1

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

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
  • For future readers: if you are using a connection pool then you can close the pool with a 'drain time'. This will force all connections to be closed. See the example https://github.com/oracle/node-oracledb/blob/v5.3.0/examples/connectionpool.js#L126-L145 – Christopher Jones Feb 02 '22 at 03:22
1

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.

Dan McGhan
  • 4,479
  • 1
  • 11
  • 15
0
var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : XX,
  host            : 'yourHost',
  user            : 'user',
  password        : 'password'
});

pool.getConnection(function(err, connection,) {
    connection.query( 'SELECT something FROM sometable', function(err, rows) {

//handler
      connection.release();//this is the important step

   });
});

Alternatively you can use: .query

pool.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  //your success handler
});

pool.query() is shortcut for pool.getConnection() + connection.query() + connection.release()

If you are looking something on exit handler:

function graceExitHandler(){
// close the db connection here.
..
} 

//when nodejs app is closing
process.on('exit', exitHandler);

//on Ctrl + C interrupt
process.on('SIGINT', exitHandler);
Sunil B N
  • 4,159
  • 1
  • 31
  • 52
0

You can catch sigint signal which is for ctrl+c event and inside that you can close your any open connections.Below is the code from our production server.

process.on('SIGINT', () => {                                                                           
    serverVariables.ServerStatics.upStatus = 0;
    console.log('Received Signal To Shutdown Server...Wait For Sometime.... [%s]', timeStamp());
    setTimeout(()=>{
            httpServer.close((e) => {                                                              
            if (e) {
                    console.log(e);
                    process.exit(0);
            }
            require('../signalHandler').cleanShutdown()
                    .then(() => {
                            console.log('SERVER SHUTDOWN SUCCESSFULL....:-) [%s]', timeStamp());
                            process.exit(0);
                    })
                    .catch((e) => {                                                                
                            console.log (e);
                            process.exit(0);                                                       
            });
    });     
    },6000);

});