如何使用C ++驱动程序重新连接到MongoDB? [英] How to re-connect to MongoDB using C++ driver?
问题描述
http://pastebin.com / jwRDhNWQ
当我重新启动MongoDB时,我可以看到正在进行新的连接。
然而,conn.isFailed()仍然为真。
这可能是因为当我重新连接时,我使用 conn
而不是& conn
当我使用& conn
如& conn.connect( localhost);
,我收到错误消息 -
错误:lvalue需要一元' ;'操作数
如何解决这个问题?即修改底层连接,以便在建立新的连接时conn.isFailed()变为false?
您应该在mongo :: DBClientConnection :: DBClientConnection构造函数中启用_autoReconnect。
http://api.mongodb.org/cplusplus/current/classmongo_1_1_d_b_client_connection.html#a6a1a348024dd302572504b7bfb6e74a2
在调用_check Connection之前,方法isfailed()返回的变量_failed不会设置。直到发送到数据库之前,才调用_checkConnection,所以作为一种替代方法,可以在调用_isFailed之前调用ping命令。但是,建议的修复是启用_autoReconnect。
I've a C++ function which saves a document to MongoDB using C++ driver. It takes connection reference as argument:
When I restart MongoDB, I can see that new connection is being made. However, conn.isFailed() remains true.
This maybe happening due to the fact that when I reconnect, I am using conn
and not &conn
When I do use &conn
as in &conn.connect("localhost");
, I get error message-
error: lvalue required as unary ‘&’ operand
How do I fix this? i.e. modify the underlying connection so that conn.isFailed() becomes false when a new connection has been established?
You should enable _autoReconnect in the mongo::DBClientConnection::DBClientConnection constructor.
http://api.mongodb.org/cplusplus/current/classmongo_1_1_d_b_client_connection.html#a6a1a348024dd302572504b7bfb6e74a2
The variable _failed returned by the method isfailed() is not set until _check Connection is called. _checkConnection is not called until something is sent to the database, so as an alternative, you could call the ping command before calling _isFailed. However, the recommended fix is to enable _autoReconnect.
这篇关于如何使用C ++驱动程序重新连接到MongoDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!