[CXX-958] Am I supposed to create the instance/connection once and pass it to every function I write? Created: 25/Jun/16  Updated: 11/Sep/19  Resolved: 26/Jun/16

Status: Closed
Project: C++ Driver
Component/s: Implementation
Affects Version/s: 3.0.1
Fix Version/s: None

Type: Task Priority: Trivial - P5
Reporter: George Thompson Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10
MS VS 2015
MongoDB 3.3.5
MongoDB C++11 Driver 3.0.1


Issue Links:
Related
related to CXX-1196 Improve mongocxx::instance documentation Closed

 Description   

Using mongoDB 2.2, every time I connected to the server within a function, I did this:

boost::scoped_ptr<mongo::ScopedDbConnection> conn(mongo::ScopedDbConnection::getScopedDbConnection ("localhost" ) )

Then I would use conn->get() and when done conn->done().

In 3.3.5, I am doing this within each function:

mongocxx::instance inst{}; mongocxx::client conn{mongocxx::uri}

This is, apparently wrong to do this every time I want to make a connection. The first time, it works perfectly. The server window outputs:

connection accepted from 127.0.0.1:57896 #30 (2 connections now open)

and dutifully ends the connection at the end of the function block

end connection 127.0.0.1:57896 (1 connection now open).

(The 1 connection is the mongo shell). But when I repeat this in a subsequent block and try a command, it throws an exception:

No suitable servers found (`serverSelectionTryOnce` set): [Failed to resolve 'localhost']: generic server error.

So, am I supposed to create the instance/connection once and pass it to every function I write (since globals are evil)?



 Comments   
Comment by George Thompson [ 26/Jun/16 ]

Thanks Andrew. CXX-890 and the Google Group discussion helped clarify what the instance class does. I have to admit to some confusion.

Comment by Andrew Morrow (Inactive) [ 26/Jun/16 ]

therefore - That is correct, you only need (and should not create more than) one instance of the instance class. Please note that we are expecting to make some changes to how this works in the upcoming 3.0.2 release, though we have not entirely worked out what will change. Please see CXX-890 for background.

Comment by George Thompson [ 26/Jun/16 ]

I understand. I only need the instance in the main program. It works fine now.

Generated at Wed Feb 07 22:00:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.