How to re-connect to MongoDB using C++ driver?

筅森魡賤 提交于 2019-12-11 15:33:19

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/8800242/how-to-re-connect-to-mongodb-using-c-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!