C++ Legacy Driver mongoDB Replicaset in Class of a DLL

99 views Asked by At

I have built a dll which includes a class implementing the mongoDB replicaset operations. Here is a summary of the class.

#include "mongo/client/dbclient.h"

mongoimp::mongoimp() {
    mongo::client::initialize();
}

mongoimp::~mongoimp() {
    mongo::client::shutdown();
}

int mongoimp::queryTRecords() {
    string errmsg;
    vector<mongo::HostAndPort> hosts = { mongo::HostAndPort("xx-a0.yyyy.com:xxxxx"), mongo::HostAndPort("xx-a1.yyyy.com:xxxxx") };
    static mongo::DBClientReplicaSet con("xx", hosts, 0);
    con.connect();
    con.auth("dbname", "username", "password", errmsg);
    auto_ptr<DBClientCursor> cursor = con.query("dbname.t", BSONObj());
    BSONObj response;
    con.logout("xx", response);
    if (cursor->more()) {
        BSONObj recordnm = cursor->nextSafe();
        return(recordnm.getIntField("lastid"));
    } else return(-1);
}

The above code is working. But here are my questions:

1) With the above setting, I can do normal mongoDB operations with the dll but since my application needs to constantly update mongoDB data (close to real-time, up to hundreds a second), I am getting error (No valid replicaset instance servers found) when updating data.

2) Only the server needs to talk to the mongoDB database. So basically I just need one connection to the database. So I want to declare the mongo::DBClientReplicaSet con as a static global variable and connect to it in the class construct function. But it seemed I cannot do it. My application cannot run at all. With that, I constantly get the following error.

Assertion failed: px != 0, file C:\Boost\include\boost-1_62\boost/smart_ptr/scoped_ptr.hpp, line 105

Does anybody know how to solve the problem?

Below is the code I tried:

static mongo::DBClientReplicaSet con("xx", { mongo::HostAndPort("xx-a0.yyyy.com:xxxxx"), mongo::HostAndPort("xx-a1.yyyy.com:xxxxx") }, 0);

mongoimp::mongoimp() {
    mongo::client::initialize();
    string errmsg;
    con.connect();
    con.auth("dbname", "username", "password", errmsg);
}

mongoimp::~mongoimp() {
    BSONObj response;
    con.logout("xx", response);
    mongo::client::shutdown();
}

int mongoimp::queryTRecords() {
    auto_ptr<DBClientCursor> cursor = con.query("dbname.t", BSONObj());
    if (cursor->more()) {
        BSONObj recordnm = cursor->nextSafe();
        return(recordnm.getIntField("lastid"));
    } else return(-1);
}

3) Last question, I noticed there is mongo/client/dbclient_rs.h" file for replicaset. But it seemed I cannot use it. With that, I am getting error for initialize() and auto_ptr cursor. How can I use the file and take full advantage of replicaset features? How can I initialize the relica set if I can use "dbclient_rs.h"? How do I do query and fetch data in that case?

Thanks a lot in advance!

1

There are 1 answers

0
Harold On

For question No. 2, I remembered the reason for the error:

You need to call mongo::client::initialize before you construct any driver objects, or BSON for that matter.

But how to make that global definition possible, I still need a solution.