如何使用C ++驱动程序重新连接到MongoDB? [英] How to re-connect to MongoDB using C++ driver?

查看:265
本文介绍了如何使用C ++驱动程序重新连接到MongoDB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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:

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?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆