[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 |
||
| Issue Links: |
|
||||||||
| 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. |
| 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 |
| Comment by George Thompson [ 26/Jun/16 ] |
|
I understand. I only need the instance in the main program. It works fine now. |