如何使用 C++ 重新连接到 MongoDB司机?
我有一个 C++ 函数,它使用 C++ 驱动程序将文档保存到 MongoDB。它将连接引用作为参数:
当我重新启动 MongoDB 时,我可以看到正在建立新的连接。 但是,conn.isFailed() 仍然为 true。
发生这种情况可能是因为当我重新连接时,我使用的是 conn
而不是 &conn
当我像 &conn.connect("localhost");
中那样使用 &conn
时,我收到错误消息 -
error: lvalue required as unary ‘&’ operand
如何解决此问题?即修改底层连接,以便在建立新连接时 conn.isFailed() 变为 false?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在 mongo::DBClientConnection::DBClientConnection 构造函数中启用 _autoReconnect。
http://api.mongodb.org/cplusplus/current/classmongo_1_1_d_b_client_connection.html#a6a1a348024dd302572504b7bfb6e74a2
方法 isfailed() 返回的变量 _failed 在调用 _check Connection 之前不会设置。在将某些内容发送到数据库之前,不会调用 _checkConnection,因此作为替代方案,您可以在调用 _isFailed 之前调用 ping 命令。但是,建议的修复方法是启用 _autoReconnect。
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.