最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to release Oracle Database connection when Node.js exits - Stack Overflow

matteradmin5PV0评论

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

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

Share Improve this question edited Feb 2, 2022 at 3:23 Christopher Jones 10.9k7 gold badges30 silver badges62 bronze badges asked Jun 5, 2018 at 5:26 Ratan Uday KumarRatan Uday Kumar 6,5427 gold badges40 silver badges55 bronze badges 1
  • stackoverflow./questions/14031763/… 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 Commented Jun 5, 2018 at 5:29
Add a ment  | 

4 Answers 4

Reset to default 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

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.

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);

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);

});

Post a comment

comment list (0)

  1. No comments so far