Couchbase PHP SDK: How to detect couchbase connection failure?

吃可爱长大的小学妹 提交于 2019-12-22 18:08:17

问题


I am using Couchbase PHP Extension to connect to Couchbase and implementing a feature that can detect if Couchbase is not responding and failover to MySQL. However, I can't figure out how to detect if Couchbase is down, I cannot find anything in their documentation for that.

Following is the code I have:

$cb = new Couchbase("$host:$port", $admin, $password, $bucket);
if (!$cb) {
    throw Exception('Cannot connect to couchbase!');        
}

Any help will be much appreciated.


回答1:


I run into the same question, and solved it this way:

$cb = @new Couchbase($host.":".$port,$username,$password,$bucket,$persistent);
if($cb->getResultCode() != COUCHBASE_SUCCESS){ 
    throw Exception('Cannot connect to couchbase!'); 
} 



回答2:


I would recommend counting errors up to a threshold and then consider the connection 'down' and try to rebuild it.

Note that part of the reason it's difficult to determine 'down' is that Couchbase, by design, would rarely completely fail and even when failed nodes do occur, failover is triggered either automatically or manually bringing that connection object back online. It's different in this regard than connections to other databases.



来源:https://stackoverflow.com/questions/11532444/couchbase-php-sdk-how-to-detect-couchbase-connection-failure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!