Couchbase PHP SDK: How to detect couchbase connect

2019-05-24 17:01发布

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.

标签: php couchbase
2条回答
等我变得足够好
2楼-- · 2019-05-24 17:30

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-05-24 17:35

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!'); 
} 
查看更多
登录 后发表回答