Details
-
Bug
-
Resolution: Won't Fix
-
Major - P3
-
None
-
2.4.3
-
None
Description
While updating an older application to the latest driver, I needed to implement a method which took a connection string and returned a MongoDatabase instance.
I ended up with this implementation:
var url = MongoUrl.Create( connectionString );
|
var serverSettings = MongoServerSettings.FromUrl( url );
|
var s = new MongoServer( serverSettings );
|
var db = s.GetDatabase( url.DatabaseName );
|
return db;
|
However, after deploying the application to production, we discovered that it was slowly leaking memory. After some investigation, I found that after calling this method 500 times, a number of objects had been created, which would not be garbage collected:

MongoServer is not disposable and I cannot find any references in the documentation about how to "clean up".
I am now using this implementation, which does not seem to leak:
var url = MongoUrl.Create( connectionString );
|
var client = new MongoClient(url);
|
var s = client.GetServer();
|
var db = s.GetDatabase( url.DatabaseName );
|
return db;
|
However, GetServer() is deprecated, so I expect it will go away at some point.
I realize that I "should" be using IMongoDatabase instead of MongoDatabase so I could just use client.GetDatabase(...) but this is a large code base, and the API of IMongoDatabase is radically different (and worse IMHO).
Attachments
Issue Links
- related to
-
CSHARP-1946 Edit documentation to clarify the proper way to create a legacy MongoServer instance
-
- Closed
-