When should i be opening and closing MongoDB connections?
Asked Answered
O

2

12

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?

Opisthognathous answered 7/2, 2012 at 7:16 Comment(2)
Norm is hardly being maintained anymore, if you want LINQ support go with official mongo driver with FluentMongo on top of it.Palmette
Beside this question of opening and closing connections on the code, I had issues with the internet provider closing connections, then my code having EndOfStreamException. I solved it by using settings.MaxConnectionIdleTime = TimeSpan.FromSeconds(30); https://mcmap.net/q/697796/-properly-shutting-down-mongodb-database-connection-from-c-2-1-driverCubage
C
11

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

Chil answered 7/2, 2012 at 9:11 Comment(2)
Agreed. In fact, you don't even really need to write a MongoSession class — storing a MongoServer instance in application scope would be appropriate. You should almost certainly call Disconnect() on this instance only once, when the MVC application closes.Earmuff
@Sean Thanks for the tip on when to close connections.Maxma
J
2

You Do NOT Need to Call Connect or Disconnect

The C# driver has a connection pool to use connections to the server efficiently. There is no need to call Connect or Disconnect; just let the driver take care of the connections (calling Connect is harmless, but calling Disconnect is bad because it closes all the connections in the connection pool).

http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/

Jerlenejermain answered 6/5, 2014 at 14:32 Comment(1)
link (now) doesn't mention anything, it's just a parent page of several others.Dee

© 2022 - 2024 — McMap. All rights reserved.