Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb nodejs native driver close connection or not

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

      });
    });
like image 901
Zhe Hu Avatar asked Apr 30 '13 21:04

Zhe Hu


People also ask

Should I close MongoDB connection?

javascript, node.js, database, mongodb In general, it is always a good practice to close the resources after they are used and before exiting the application.

Do MongoDB connections close automatically?

1.3 close() method in the Mongo databaseThe server will automatically close the cursors that have no remaining results and the cursors that have been idle for a time. Here is what the query syntax will look like.

Should I close DB connection after query?

For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.

Do you need to close Mongoose connection?

As we have already seen, the general best practice is to open your connection at application start up, and keep it open. However, there are times when you will want to close the connection.


1 Answers

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

like image 152
Jason Avatar answered Oct 19 '22 23:10

Jason