最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

});

发布评论

评论列表(0)

  1. 暂无评论