2013-11-03 44 views
0

嘗試mongodb全局超時等仍被我的PHP腳本中的find()查詢忽略。 我想要一個findOne({...})或find({...})查找,並在超時之前等待數據庫服務器的最長20ms。PHP/MongoDB:find()忽略超時設置

如何確保PHP不會將此設置用作軟限制?它仍然被忽略,甚至在5秒後處理答案。

這是一個PHP mongo驅動程序錯誤?

實施例:

MongoCursor::$timeout=20; 
$nosql_server=new Mongo('mongodb://user:[email protected]'.implode(",",$arr_replicas).'',array("replicaSet" => "gmt","timeout"=>10)) OR troubles("too slow to connect"); 
$nosql_db=$nosql_server->selectDB('aDB'); 
$nosql_collection_mcol=$nosql_db->mcol; 
$testFind=$nosql_collection_mcol->find(array('crit'=>123)); 
//If PHP considered the MongoCursor::$timeout, I'd expect the prev. line to be skipped or throwing a mongo/timeout exception if DB does not return the find result cursor ready within 20ms. 
//However, I arrive with this line after seconds, without exception whenever the DB has some lock or delay, without skipping previous line. 
+1

您是否在您的php.ini配置或代碼中設置超時?無論哪種方式,我們可以看到您使用的代碼來執行查找嗎? – halfer

+0

我在腳本中設置了超時。編輯:您現在可以找到示例代碼。 – ledy

回答

0

在PHP文檔$timeout以下是用於光標超時的解釋:

導致該擷取結果拋出 MongoCursorTimeoutException如果查詢需要更長的時間的方法比指定的毫秒數爲 。

相信超時指的是操作上執行光標(例如getNext())。

0

不要這樣做:

MongoCursor::$timeout=20; 

即調用一個靜態方法,不會給你帶來任何好的AFAIK。

你需要什麼意識到的是,在你的代碼示例,$ testFind的MongoCursor對象。因此,在代碼片段,你給了,你應該做的是,以設置$ testFind MongoCursor的超時一切後補充一點:

$testFind->timeout(100); 

注意:如果你要處理$ testFind作爲一個數組,你需要做的:

$testFindArray = iterator_to_array($testFind); 

那一個扔我要一段時間的循環。希望這可以幫助某人。

0

注意readPreference屬性。可能的值是:

MongoClient::RP_PRIMARY 
MongoClient::RP_PRIMARY_PREFERRED  
MongoClient::RP_SECONDARY 
MongoClient::RP_SECONDARY_PREFERRED 
MongoClient::RP_NEAREST