for(var i=0;i<100;i++)
{
using(var mongo1 = new Mongo())
using(var mongo2 = new Mongo())
{
mongo1.Connect();
mongo2.Connect();
}
}
Ive got:
Wed Jun 02 20:54:21 connection accepted from 127.0.0.1:58214 #1
Wed Jun 02 20:54:21 connection accepted from 127.0.0.1:58215 #2
Wed Jun 02 20:54:21 MessagingPort recv() errno:0 No error 127.0.0.1:58214
Wed Jun 02 20:54:21 end connection 127.0.0.1:58214
Wed Jun 02 20:54:21 MessagingPort recv() errno:0 No error 127.0.0.1:58215
Wed Jun 02 20:54:21 end connection 127.0.0.1:58215
Are you really sure you close the the Mongo after each request?
1. Creating a singleton object to use one connection to retrieve the data
2. Create and close a connection for each CRUD action
For web applications I believe the singleton pattern is preferred since each web server has one open connection. Here's a code example:
public sealed class MongoConnection
{
private static volatile Mongo instance;
private static object syncRoot = new Object();
private MongoConnection() { }
public static Mongo Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
instance = new Mongo(ConfigurationManager.AppSettings.Get("MongoDB"));
instance.Connect();
}
}
}
return instance;
}
}
}
Jeremy
As you can the in my log ive created 200 Mongo objects, but the server
logged only two connections.
On 14.06.2010 14:32, Flavien C wrote:
>> {