|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-12-13 03:26 UTC] xqpmjh at gmail dot com
Description:
------------
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
Mongo('mongodb://kim:kim@localhost:27018,localhost:27019,localhost:27020/test');
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
happen...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 21:00:01 2025 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.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 :)