如何使用 C++ 重新连接到 MongoDB司机?

发布于 2024-12-26 01:30:03 字数 524 浏览 0 评论 0原文

我有一个 C++ 函数,它使用 C++ 驱动程序将文档保存到 MongoDB。它将连接引用作为参数:

http://pastebin.com/jwRDhNWQ

当我重新启动 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:

http://pastebin.com/jwRDhNWQ

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野味少女 2025-01-02 01:30:03

您应该在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文