Search code examples
pythonc++mongodbmongo-cxx-driver

MongoDB Python and C++ clients - error with multiple instances


I'm still new to the MongoDB. My test C++ application is composed from a number of object files, and two of them have their own MongoDB instances. I've found that was a mistake, cause I've got an exception:

terminate called after throwing an instance of 'mongocxx::v_noabi::logic_error'
  what():  cannot create a mongocxx::instance object if one has already been created
Aborted (core dumped)

So, I'll try to define a single MongoDB instance in this application.

And now I worry about my another application - it's top-level program in Python, which loads a number of dynamic libraries, written in C++ and having their own MongoDB instances. Where should I define the MongoDB instance - in the top-level code, in each library, in one of the libraries?


Solution

  • You should create one shared library which manages a singleton instance of mongocxx::instance and have all of the other libraries which need to use the driver access that singleton via some common API. Please see the instance management example.