0

I am upgrading my mongodb c++ client code from legacy driver and 26compat driver to r3.0.2 driver. The scopedconnection pool class is removed now. My question is how to connect to replica set with connection pool. My current code is:

 //only call it once in my app
 mongocxx::instance inst{};
 //call it in anywhere I need 
 mongocxx::client conn{mongocxx::uri{mongodb://i-e6ql0k8k,i-exiv5yox,i-sfdxzsjr/?replicaSet=rs1}};

use the default maximum pool size 100 value. But I do not know if my way is the correct or not?

acm
  • 12,183
  • 5
  • 39
  • 68
Dean Chen
  • 3,800
  • 8
  • 45
  • 70

1 Answers1

1

You'll want to use the mongocxx::pool class. The URI looks fine – as long as you have the replicaSet option, the driver understands to connect in replica set mode.

xdg
  • 2,975
  • 19
  • 17
  • oh, thanks. Could you give me a simple example or url of doc to show how to use this pool class? I am new to r3.0.2 driver. I guess I should keep the pool object as a singleton object in process and then call its acquire method to get client object every time in all my threads. Am I right? – Dean Chen Nov 16 '16 at 17:04
  • That's right. Create the pool early and check out clients as needed in threads. We have a ticket to write an example, but haven't gotten around to it quite yet. – xdg Nov 17 '16 at 14:21
  • where is your ticket? I can provide my example code for that. – Dean Chen Nov 18 '16 at 07:25
  • added my code in it. It has been used in my real project. – Dean Chen Nov 22 '16 at 14:45