[CSHARP-970] MongoServer.GetDatabase does not return the same instance Created: 11/May/14  Updated: 02/Apr/15  Resolved: 12/May/14

Status: Closed
Project: C# Driver
Component/s: Documentation
Affects Version/s: 1.9
Fix Version/s: 1.9.1

Type: Bug Priority: Minor - P4
Reporter: Ben Foster Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

According to the docs, MongoServer.GetDatabase should return the same instance when the same database settings (e.g. database name) are used. However, the following test fails:

void describe_get_database()
{
MongoServer server = null;
MongoDatabase db = null;
MongoDatabase db2 = null;
string dbName = null;

before = () =>

{ var client = new MongoClient("mongodb://localhost"); server = client.GetServer(); dbName = "test"; db = server.GetDatabase(dbName); }

;

act = () => db2 = server.GetDatabase(dbName);

context["when the database name is the same"] = () =>

{ it["should return the same database instance"] = () => db2.should_be_same(db); }

;
}

Have I misunderstood the documentation as looking through the source code it appears that GetDatabase always returns a new MongoDatabase instance.



 Comments   
Comment by Robert Stam [ 12/May/14 ]

Corrected XML doc comments.

Comment by Githook User [ 12/May/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-970: Correct doc comments for GetDatabase.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/e39d6335d7e2113cd85bc6e294d29bb21eeb5fcf

Comment by Githook User [ 12/May/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-970: Correct doc comments for GetDatabase.
Branch: v1.x
https://github.com/mongodb/mongo-csharp-driver/commit/259b762292a8b3217fbf84ccd410908e71a18a89

Comment by Robert Stam [ 11/May/14 ]

If you can hold on to the MongoDatabase instance and use it repeatedly you will create slightly less work for the garbage collector.

But MongoDatabase is a light weight class that would be cheap to create and garbage collect, so either way is fine.

Comment by Ben Foster [ 11/May/14 ]

Hi Robert,

Is there any reason why we should keep hold of the MongoDatabase instance
or is it fine to create it each time we make a query?

Thanks
Ben

Comment by Robert Stam [ 11/May/14 ]

Sorry. The documentation is wrong. GetDatabase will return a new instance of MongoDatabase each time it is called.

We will get the documentation corrected.

Thanks for reporting this.

Generated at Wed Feb 07 21:38:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.