Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should i be opening and closing MongoDB connections?

i am very new to MongoDB and NoSQL in general and i've just started building a site with MongoDB / Norm / ASP.NET MVC 3.

I am wondering how i should be scoping the connections to my Mongo database.

Right now i have a Basecontroller that instanciates the MongoSession and onActionExecuted i dispose it so all my deriving controllers will have access to my MongoSession. The MongoSession class opens a connection in its constructor and disposes it on Dispose(), the way it's working today.

private IMongo _mongo;

public MongoSession()
{         
    _mongo = Mongo.Create("connString");      
} 

public void Dispose()
{
    _mongo.Dispose();
}

I am a bit worried it might be holding connections open too long if i am doing other stuff aswell in the controllers.

Is that approach enought to not risking holding too many connections open or should i be doing something more like the example method below?

   public void Add<T>(T item) where T : class, new()
   {
       using (var mongo = Mongo.Create("connString"))
       {
         mongo.GetCollection<T>().Insert(item); 
       }
   }

Another follow up question is:

Are opening and closing MongoDB connections through Norm "expensive" operations?

like image 267
Kimpo Avatar asked Feb 07 '12 07:02

Kimpo


People also ask

When 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.

Should I close connection after each query?

Open the connections just when you need to execute a query and close it as early as possible. So my solution is, YES. Show activity on this post. if your application doesn't close connection properly may lead to some issues like the connection pool maxing out.

Do you need to close Mongoose connection?

You should close a mongoose connection when a Node POSIX signal is happening. SIGINT process is triggered when Ctrl-C has been pressed on terminal or a server shutdown. Another possible scenario is to close a connection when a data streaming is done.

How do I keep my MongoDB connection open?

The most simple way to allow MongoDB to reconnect is to define a reconnectTries in an options when passing it into MongoClient . Any time a CRUD operation times out, it will use the parameters passed into MongoClient to decide how to retry (reconnect). Setting the option to Number.


1 Answers

I would leave the connection open as re-creating the connection is costly. Mongo is fine with lots of connections, open for a long time. What you ideally should do is to share the connection with all parts of your application as a persistent connection. The C# driver should be clever enough to do this itself, so that it does not create too many connections, as internally it uses "connection pooling" that makes it even re-use connections. The docs say: "The connections to the server are handled automatically behind the scenes (a connection pool is used to increase efficiency)."

cheers, Derick

like image 69
Derick Avatar answered Sep 21 '22 05:09

Derick