8

Is it a good practice in nodejs to open mongodb connection per each request, and close it in the callback?

app.get('/some_route', function(){
      MongoClient.connect(url,function(err, db){
          //some db query with callback
          db.collection("some_collection").findOne(doc, function(err,item){
               if(err){
                      res.send(err);
                      //close db connection 
                      db.close();
                }else{
                      //do something with item
                      res.send(item);
                      //close db connection 
                      db.close();
                }

      });
    });

Some said that opening/closing mongodb connection on each request isn't necessary, since once opened, a pool of connections can be shared.

The question is how to maintain and share that pool? Is mongoose doing that automatically already?

Especially, on mongodb timeout or disconnect, does it need to be reconnected?

I find contradictory answers here close mongodb connection per request or not

Almost all the online doc nodejs mongodb native driver and example code I read, a db.open() is paired with db.close() somewhere in the callback.

Because if a pool of connection is shared, one might code According to christkv's answer, one might code:

var p_db=null;
var c_opt = {server:{auto_reconnect:true}};

app.get('/some_route', function(){
      //pseudo code
    if (!p_db){
           MongoClient.connect(url, c_opt, function(err,db){
                  p_db = db;
                  p_db.collection("some_collection").findOne(doc, function(err,item){
                  if(err){
                      res.send(err);                          
                  }else{
                      //do something with item
                      res.send(item);
                  }

             });
           });
       }else {
          p_db.collection("some_collection").findOne(doc, function(err,item){
               if(err){
                      res.send(err);
                }else{
                      //do something with item
                      res.send(item);
                }

      });
    });
Community
  • 1
  • 1
Zhe Hu
  • 3,777
  • 4
  • 32
  • 45
  • I read the mongoose src few months ago, they do open one connection per collection, so it is just cached in variable. not sure they may changed. I do write a module for connection and run it before app.listen. – wayne Apr 30 '13 at 23:42

2 Answers2

9

According to a major contributor to the driver source, It's best to connect to the database at startup and keep reusing that same connection for each request.

The mongodb native driver has a connection pool which it maintains internally and currently defaults to a maximum of 5 open connections. You can configure the maximum number of connections via the maxPoolSize option. You can also configure the connection to auto reconnect with the auto_reconnect option.

See the documentation here

Jason
  • 153
  • 1
  • 7
5

You don't have to do anything to reconnect as the driver will attempt to reconnect on failure. While it waits for the reconnect to happen it will buffer all operations happening in between and replay them once the connection is up. If you want to control this yourself you can listen to the "close" event on the db instance and handle reconnection manually. On reconnect the db object will still be viable as a db is really just a wrapper around the shared connection pool and does not contain it's own separate connection logic.

christkv
  • 4,370
  • 23
  • 21