There was the same problem. I can confirm that it is associated with the connection pool in the driver, and I did not find a valuable option. I can also confirm that this happens for the first new MongoClient () requests a few hours after restarting db.
I did to surround my new MongoClient () with try catch and repeat it. For instance. something like that:
try { $NoSQLDBMS_Connection = new MongoClient( $NoSQLDBMS_Host ); $NoSQLDBMS_Database = $NoSQLDBMS_Connection->selectDB( $NoSQLDBMS_DBName ); } catch( Exception $e ) { // retry, mostly when mongodb has been restarted in order to get a new connection $MaxRetries = 5; for( $Counts = 1; $Counts <= $MaxRetries; $Counts ++ ) { try { $NoSQLDBMS_Connection = new MongoClient( $NoSQLDBMS_Host ); $NoSQLDBMS_Database = $NoSQLDBMS_Connection->selectDB( $NoSQLDBMS_DBName ); } catch( Exception $e ) { continue; } return; } // do something fancy here if mongodb is not reachable at all }
Retry to max. 5 times is just paranoia. I have never experienced that more than one attempt has ever been required.
Hope this helps.
source share