|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60508 When replicaSet is set, Mongo->connected is always TRUE
Submitted: 2011-12-13 03:26 UTC Modified: 2012-11-29 17:42 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: xqpmjh at gmail dot com Assigned: derick (profile)
Status: Closed Package: mongo (PECL)
PHP Version: 5.3.8 OS: winxp
Private report: No CVE-ID: None
 [2011-12-13 03:26 UTC] xqpmjh at gmail dot com
I am not sure if this is a bug, bug it's just very strange thing.

The problem is :
when I try to setup my replica set on my local pc. Trying connect to mongo with 
the "replicaSet" option before actually 
launch any instance of mongo:

$mo = new 
var_dump($mo->connected); // it output TRUE... which is unacceptable :p

But in fact the connection had not been established, because I didn't really 
launch any mongo process.
The exception "couldn't get a connection to any server" will still be thrown, 
yes, but only until I do some queries like :

$entities = $myCollection->find();
try {
    while ($entities->hasNext()) {
        $row = $entities->getNext();
        $result[] = $row;
} catch (exception $e) {
    echo 'exception thrown here...';

The mainly uncomfortable thing of this is that, the exception is unable to 
capture, and don't know when it will be thrown. 
Especially when trying to check connection status at the very beginning before 
doing any other things.

But if I remove the "replicaSet" option and try to connect again, this time, the 
exception will be thrown immediately, but 
with a message of "Unknown error", which is, just can't tell anything about what 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-16 19:02 UTC]
This is intentional, although I can see it being annoying behavior.  As long as all the addresses resolve and all of the other trivial stuff goes okay (you don't run out of memory or something), Mongo::__construct() will set connected to true.

I could see making connected=false if it can't reach anyone, but should it be true if you can only reach secondaries?  If you can only reach an arbiter?  I can't think of a way of making connected=boolean make sense in the context of replica sets.

If you want to catch this early, you could do $db->command(array("ping"=>1)) or something after connecting.
 [2011-12-19 03:22 UTC] xqpmjh at gmail dot com
Yes, $db->command(array("ping"=>1)) is a proper way of checking the connection. 
But I still think that there could be a better solution.

If we looking at the word "connected", which usually means we can "talk" to each other, then connected=true means you can both read/write to the server.
Does it mean that you are able to connect to a "primary"?

I am thinking that even some thrown exceptions are better then just a true/false when, if someone can only read, or only see the arbiter, but unable to write...

Just the modest proposal, happy PHP :)
 [2012-11-29 17:42 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2012-11-29 17:42 UTC]
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at

This should be fixed in the 1.3.0 release. If you continue finding problems with this, please file an issue in JIRA:
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Jun 15 14:01:24 2021 UTC