Last Updated
Viewed 09 Times

There's probably an obvious answer to this, but I can't seem to find it. I'm using Mongoose in my node app to connect to a MongoDB cluster. Is there any way to monitor the state of the client-side connection pool that Mongoose uses? Like a number of connection in the pool, number of connections currently checked-out from the pool, the rate at which connections are checked-out, etc? Bonus points if there's already a well established solution of exporting these metrics in Prometheus format.

If I receive a SIGINT/SIGTERM (e.g. ctrl+c) then I must gracefully stop my app and close all connections to the mongodb server.

Most documentation/tutorials state that to stop a connection (or pool of connections), I must use mongoose.disconnect() - which calls .close() on connections in the pool.

However all those docs also say this should be done after all pending writes are completed.

How can I know this when I receive a signal event (e.g. SIGINT, SIGTERM, etc.)? Does mongoose have graceful shutdown functionality, or will I lose data when I call disconnect()?

(I'm using the latest bits.)

my app.js contains,

 exports.getDatabaseConnection = function (dbname, uname, psw) {
mongoose.disconnect();
     mongoose.connect('mongodb://' + uname + ':' + psw + '@localhost:27017/' + dbname);
    }

and index.js is

router.post('/chooseDb', function (req, res) {
      var temp = req.body.country;
      if (temp === "india") {
        var uname = 'abc';
        var psw = 'abc123';
        var dbname = 'demoDB';
        app.getDatabaseConnection(dbname, uname, psw);
        res.json('done');
      }
       else if (temp === "finland") {
    var uname = 'xyz';
    var psw = 'xyz123';
    var dbname = 'testDB';
    app.getDatabaseConnection(dbname, uname, psw);
    res.json('done');
  } 
    })

when first i connect to india then it works fine but second time when i call an chooseDb api with finland it gives an error like, Error: Trying to open unclosed connection. for mongoose.createConnection i tried schema like

 var mongoose = require('mongoose'); 
    var schema = mongoose.Schema; 
    var db=require('../app.js'); 
    var garageSchema = new schema({ id: String, name: String, address: String }); 
module.exports = db.model('garage', garageSchema); 

which gives me an error db.model is not function

Similar Question 3 (5 solutions) : Mongoose Connection

Similar Question 6 (1 solutions) : How to make a Mongoose connection fail gracefully

Similar Question 7 (2 solutions) : How to handle Mongoose DB connection interruptions

Similar Question 9 (1 solutions) : how to kill or close specific connection on mongoose

cc