27

What is the best practice for managing the MongoServer class life cycle? Should I create one and close it at the end of each request or should it be kept as a singleton for the entire life of the app using something like StructureMap?

Any help is appreciate.

Roman
  • 10,309
  • 17
  • 66
  • 101

3 Answers3

24

In the official documentation it is stated that MongoServer, MongoDatabase, and MongoCollection are thread safe, and that you're supposed to create one single MongoServer for each database that you connect to.

Thus, MongoServer, MongoDatabase, and MongoCollection can safely be configured to be singletons. MongoServer will even help enforcing this by returning the same MongoDatabase instance for successive calls, and MongoDatabase will do the same thing for MongoCollections.

I.e. your MongoServer instance can safely be configured to have a singleton lifestyle in your IoC container, and you might as well set up injection for MongoDatabase and maybe even MongoCollection as well.

I'm using this strategy with Windsor myself - you can see my MongoInstaller here: https://gist.github.com/2427676 - it allows my classes to just go ahead and do this:

public class SomeClass
{
    public SomeClass(MongoCollection<Person> people)
    { ... }
}

in order to have a collection injected, nice and ready to use.

mookid8000
  • 18,258
  • 2
  • 39
  • 63
  • Thank you for sharing the WindsorInstaller. The code is somewhat old now and I was wondering if you have an updated version for the new mongodriver 2.0? I tried to upgrade it myself, but I am not sure if there is a better way?` https://gist.github.com/kri5t/afcba8068badb62f5c9b – Kristian Barrett Mar 22 '16 at 10:59
8

The C# driver manages connections to the server automatically (it uses a connection pool). There is no need to call server.Connect as the driver connects automatically. Don't call server.Disconnect as that closes all connections in the connection pool and interferes with efficient connection pooling.

As far as managing the lifecycle of the MongoServer instance you are free to store it in a static variable and use it for the lifetime of your process (and share it across threads, it is thread-safe). Alternatively, you can just call MongoServer.Create again whenever you need to get the server instance. As long as you keep calling MongoServer.Create with the same connection string you will keep getting back the same MongoServer instance.

Robert Stam
  • 12,039
  • 2
  • 39
  • 36
3

Inject it using any IOC container (structuremap, Windsor etc.) and keep its lifetime to be on per request basis.

Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • So that means create an instance of the server per request thread. I assume the driver handles the connection pool. Is that correct? – Roman Apr 20 '12 at 07:39
  • 1
    Yes, the `MongoServer` manages a connection pool and is NOT meant to be instantiated over and over. See http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-TheC%23Driver for more info ("You will create one instance of this class for each server you connect to. The connections to the server are handled automatically behind the scenes (a connection pool is used to increase efficiency).") – mookid8000 Apr 20 '12 at 20:15