2

Windows 7 SP1
MSVS 2010
MongoDB 2.2.0

骨架代码:

// Successful connection at the start.
mongo::DBClientConnection client_connection(true); // autoreconnect = true

// But (say) by the time we get here mongo has shut down.
try
{
    cursor = client_connection.query(...)
}
catch(const mongo::DBException& ex)
{
    cout << "DBException: " << ex.what() << "//" << ex.toString() << endl;
}

控制台首先打印:

Thu Apr 25 14:03:09 Socket say send() errno:10054 An existing connection was forcibly closed by the remote host. 127.0.0.1:27017

然后捕获 DBException:

DBException: socket exception [SEND_ERROR] for 127.0.0.1:27017 9001 socket exception [2] server [127.0.0.1:27017]

尝试重新连接:

Thu Apr 25 14:03:09 trying reconnect to localhost:27017
Thu Apr 25 14:03:10 reconnect localhost:27017 failed couldn't connect to server localhost:27017

我得到:

在此处输入图像描述

我的问题:我将如何处理重新连接失败,以便我可以优雅地处理它?

4

0 回答 0