C# Driver - Connection Pooling

2,596 views
Skip to first unread message

Gee

unread,
Dec 28, 2011, 2:07:19 PM12/28/11
to mongodb-user
Hi

I am using the official C# driver (v1.2) in a benchmarking
application. The code works fine until I use multiple threads (where
the driver is pooling connections, essentially creating a bottleneck).

Is there a way to disable connection pooling so I can use a more
traditional Connection.Open() --> Connection.Close() approach?


Thanks
G

Robert Stam

unread,
Dec 28, 2011, 2:41:41 PM12/28/11
to mongod...@googlegroups.com
There's no way to turn connection pooling off, but you can make the
connection pool bigger. The easiest way to do that is in the
connection string:

var connectionString = "mongodb://localhost/?safe=true;maxpoolsize=200";
var server = MongoServer.Create(connectionString);

The default pool size is 100. If you are using less than 100 threads
then there cannot be any contention for the connections. If your
threads are using MongoDB intensively you could set the connection
pool size equal to the number of threads you are using.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>

Robert Stam

unread,
Jan 4, 2012, 2:17:24 PM1/4/12
to mongod...@googlegroups.com
You should probably never call Disconnect. It creates havoc with connection pooling as it closes all existing connections.

Just call MongoServer.Create and go from there and let the driver do its thing. If necessary set any of the parameters that control the connection pool (max size, min size, etc...).

Don't think of an instance of MongoServer as a connection. It's a proxy object for the server as a whole. The connections themselves are handled internally.

Robert

On Wed, Jan 4, 2012 at 2:11 PM, Gee <g.p...@gmail.com> wrote:
Hi Robert

Thank you for getting back to me.  This is useful information, but it
looks like the error was at my end.  I don't think I was disposing of
connection objects properly when aborting my threads.  Having said
that, I'd still like to be able to disable connection pooling or at
least flush the connections!  (Hint hint!)

Off the top of my head (remember, NYE has come and gone since then!),
the thread's "main" should've looked something like this:


Server conn = null;

try
{
  conn = Server.Create(connection_string);

  ...
  ..
  .
}
catch (ThreadAbortException ex)
{
 // ok to ignore
}
catch (Exception error)
{
 throw error;
}
finally
{
 if(conn != null)
 {
   conn.Disconnect();

Gee

unread,
Jan 4, 2012, 2:11:05 PM1/4/12
to mongodb-user
Hi Robert

Thank you for getting back to me. This is useful information, but it
looks like the error was at my end. I don't think I was disposing of
connection objects properly when aborting my threads. Having said
that, I'd still like to be able to disable connection pooling or at
least flush the connections! (Hint hint!)

Off the top of my head (remember, NYE has come and gone since then!),
the thread's "main" should've looked something like this:


Server conn = null;

try
{
conn = Server.Create(connection_string);

...
..
.
}
catch (ThreadAbortException ex)
{
// ok to ignore
}
catch (Exception error)
{
throw error;
}
finally
{
if(conn != null)
{
conn.Disconnect();
}
}


On Dec 28 2011, 7:41 pm, Robert Stam <rob...@10gen.com> wrote:
Reply all
Reply to author
Forward
0 new messages