Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-958

Am I supposed to create the instance/connection once and pass it to every function I write?

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Trivial - P5 Trivial - P5
    • None
    • Affects Version/s: 3.0.1
    • Component/s: Implementation
    • Labels:
      None
    • Environment:
      Windows 10
      MS VS 2015
      MongoDB 3.3.5
      MongoDB C++11 Driver 3.0.1

      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)?

            Assignee:
            Unassigned Unassigned
            Reporter:
            therefore George Thompson
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: