When should i be opening and closing MongoDB connections? When should i be opening and closing MongoDB connections? mongodb mongodb

When should i be opening and closing MongoDB connections?


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


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/