Using connection pooling

599 views
Skip to first unread message

Dave N

unread,
Jun 2, 2010, 12:36:59 PM6/2/10
to mongodb-csharp
I'm using MongoDb for a backend to a web service. My CRUD operations
use a private Database Db, which is defined as:

private Database Db
{
get
{
if(_db != null)
{
return _db;
}

if(string.IsNullOrEmpty(RepositoryConfiguration))
{
throw new ArgumentNullException("Connection string
is missing");
}

_server = new Mongo(RepositoryConfiguration);
_server.Connect();
_db = _server.GetDatabase(DatabaseName);

return _db;
}
}

My mongod console looks like this:

Wed Jun 02 10:15:17 end connection 10.0.105.51:60053
Wed Jun 02 11:34:42 connection accepted from 10.0.105.49:62206 #3
Wed Jun 02 11:34:42 connection accepted from 10.0.105.49:62207 #4
Wed Jun 02 11:34:49 connection accepted from 10.0.105.49:62208 #5
Wed Jun 02 11:34:49 connection accepted from 10.0.105.49:62209 #6
Wed Jun 02 11:34:49 connection accepted from 10.0.105.49:62210 #7
Wed Jun 02 11:34:49 connection accepted from 10.0.105.49:62211 #8
Wed Jun 02 11:34:49 connection accepted from 10.0.105.49:62212 #9
Wed Jun 02 11:34:49 connection accepted from 10.0.105.49:62213 #10
Wed Jun 02 11:34:49 connection accepted from 10.0.105.49:62214 #11
Wed Jun 02 11:34:49 connection accepted from 10.0.105.49:62215 #12
Wed Jun 02 11:34:49 connection accepted from 10.0.105.49:62216 #13
Wed Jun 02 11:34:49 connection accepted from 10.0.105.49:62217 #14
Wed Jun 02 11:34:49 connection accepted from 10.0.105.49:62218 #15
Wed Jun 02 11:35:41 connection accepted from 10.0.105.49:62219 #16
Wed Jun 02 11:35:42 insert Resources.caseResources.chunks 234ms
Wed Jun 02 11:35:42 connection accepted from 10.0.105.49:62220 #17
Wed Jun 02 11:35:46 connection accepted from 10.0.105.49:62221 #18
Wed Jun 02 11:35:46 connection accepted from 10.0.105.49:62222 #19
Wed Jun 02 11:35:46 connection accepted from 10.0.105.49:62223 #20
Wed Jun 02 11:35:46 connection accepted from 10.0.105.49:62224 #21
Wed Jun 02 11:35:46 connection accepted from 10.0.105.49:62225 #22
Wed Jun 02 11:35:46 connection accepted from 10.0.105.49:62226 #23
Wed Jun 02 11:35:46 connection accepted from 10.0.105.49:62227 #24
Wed Jun 02 11:35:46 connection accepted from 10.0.105.49:62228 #25
Wed Jun 02 11:35:46 connection accepted from 10.0.105.49:62229 #26
Wed Jun 02 11:35:46 connection accepted from 10.0.105.49:62230 #27
Wed Jun 02 11:35:46 connection accepted from 10.0.105.49:62231 #28
Wed Jun 02 11:43:43 connection accepted from 10.0.105.49:62233 #29
Wed Jun 02 11:43:43 insert Resources.caseResources.chunks 265ms
Wed Jun 02 11:43:43 connection accepted from 10.0.105.49:62234 #30


I'm not sure what those #1 - #30 are, but I'm guessing they are
connections that are not getting reused. If that's right, how do I
use connection pooling to do this right? Are there examples out there
that show it done right?

Thanks!

Dave

Steve Wagner

unread,
Jun 2, 2010, 2:47:41 PM6/2/10
to mongodb...@googlegroups.com
Hi Dave, short question is _server static?

Steve Wagner

unread,
Jun 2, 2010, 2:56:59 PM6/2/10
to mongodb...@googlegroups.com
I am not able to reproduce it. When i do:

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?

Dave N

unread,
Jun 3, 2010, 11:27:18 AM6/3/10
to mongodb-csharp
Well, it looks like I was not properly closing my Mongos. My bad.

So I guess there are dumb questions...

Thanks-

Dave

Jeremy Irish

unread,
Jun 3, 2010, 11:52:55 AM6/3/10
to mongodb...@googlegroups.com
What would be a best practice for a web site?

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

craiggwilson

unread,
Jun 3, 2010, 1:22:32 PM6/3/10
to mongodb-csharp
I would approach Mongo in the same fashion as you would an OR/M
context. Open one at the beginning of the request and close it at the
end. Instantiating a Mongo is very fast, so no build up cost.

Steve Wagner

unread,
Jun 3, 2010, 3:40:28 PM6/3/10
to mongodb...@googlegroups.com
If you use a single Mongo object you create a bottle neck. Each Mongo
has only assigned one connection. On connection can only do one
operation at each time. So if you have 100 requests at the same time
they each waiting for another to be done. The connection pool is
application wide, so you could create as much mongos as you like.

Sam Corder

unread,
Jun 3, 2010, 12:26:59 PM6/3/10
to mongodb...@googlegroups.com
Treat it the same way you would an ADO connection to sql server.  I would instantiate a single mongo instance inside of my data layer and share it among the objects in it.  You could go one per crud operation but that wouldn't be as efficient.  The overhead in creating a mongo object isn't huge but it isn't free either.  

I wouldn't go the singleton route.  The web server is multi-threaded and will service more than one request at a time.  The driver is thread safe but only one thread can send a message to the database at a time.  That will cause requests to queue up in your web server as they fight over the mongo instance.

Sam Corder

unread,
Jun 2, 2010, 12:41:56 PM6/2/10
to mongodb...@googlegroups.com
Can you post your configuration string?

Flavien C

unread,
Jun 14, 2010, 8:32:44 AM6/14/10
to mongodb-csharp
But if you do that, does the driver leverage connection pooling?
> >> Dave- Hide quoted text -
>
> - Show quoted text -

Steve Wagner

unread,
Jun 14, 2010, 8:40:42 AM6/14/10
to mongodb...@googlegroups.com
No, because while creation of Mongo it borrows a connection from pool
which it owns until it is disposed. So after the using block the
connection is back into the pool.

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:
>> {

Flavien C

unread,
Jun 14, 2010, 9:00:24 AM6/14/10
to mongodb-csharp
So the answer would be yes, it does use connection pooling, right ?
So mongo.Connect() doesn't always open a new connection, in the case
of a pooled connection, it is already open, is that correct ? And
Disconnect / Dispose doesn't disconnect if it's a pooled connection,
it just returns it to the pool ?
> >>           {- Hide quoted text -

Steve Wagner

unread,
Jun 14, 2010, 9:07:13 AM6/14/10
to mongodb...@googlegroups.com
Yep that is correct :-)

Diego F.

unread,
Jun 16, 2010, 8:44:29 PM6/16/10
to mongodb-csharp
While the best idea is to dispose the Mongo after each use, you still
have to option to use it in the scope of the HttpContext. If you use a
DI/IoC framework like StructureMap you can setup the framework to
handle the creation, scope and disposing of the object. It works great
for me and keeps me from having to dispose the objects everywhere I
use them.

StructureMap configuration:

ObjectFactory.Initialize(p =>
{
p.For<Mongo>().HybridHttpOrThreadLocalScoped().Use(
() => new Mongo()
);
}

StructureMap usage:

StructureMap.ObjectFactory.GetInstance<Mongo>(); // You can
do some magic with extension methods, helpers, or whatever your
imagination tells you.

Steve Wagner

unread,
Jun 17, 2010, 2:41:20 AM6/17/10
to mongodb...@googlegroups.com
Yep that is right, open a mongo per request is a good idea.

ANIL BABU Mandla

unread,
Apr 29, 2013, 8:08:53 AM4/29/13
to mongodb...@googlegroups.com, jer...@groundspeak.com
How can i write connction string using MONGODB in ASP.NET (or) Web.config file???????
Reply all
Reply to author
Forward
0 new messages