C++传统驱动程序mongoDB复制类的DLL类

问题描述:

我已经构建了一个包含实现mongoDB复制集操作的类的dll。这是该课程的总结。C++传统驱动程序mongoDB复制类的DLL类

#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); 
} 

上述代码正在工作。但这里是我的问题:

1)使用上述设置,我可以使用dll执行正常的mongoDB操作,但由于我的应用程序需要不断更新mongoDB数据(接近实时,高达几百秒),更新数据时出现错误(找不到有效的复制服务器实例服务器)。

2)只有服务器需要与mongoDB数据库交谈。所以基本上我只需要一个连接到数据库。所以我想将mongo :: DBClientReplicaSet con声明为静态全局变量,并在类构造函数中将其连接到它。但似乎我做不到。我的应用程序根本无法运行。这样,我不断得到以下错误。

断言失败:PX = 0,文件C:\升压\包括\升压1_62 \升压/ smart_ptr/scoped_ptr.hpp,线105

是否有人知道如何解决这个问题?

下面是我试过的代码:。

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)最后一个问题,我注意到有蒙戈/客户/ dbclient_rs.h”文件replicaset但它似乎我不能使用它,就这样,我如何使用该文件并充分利用replicaset功能?如果我可以使用“dbclient_rs.h”,如何初始化relica集?如何查询和获取在这种情况下的数据?

非常感谢!

Fo第二个问题,我想起了出现错误的原因:

在构建任何驱动程序对象或BSON之前,需要调用mongo :: client :: initialize。

但是,如何使全局定义成为可能,我仍然需要一个解决方案。