Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | SDKs | SDKs

PHP Couchbase/ext 1.0 Connecting to Multiple Servers in case of Failure

7 replies [Last post]
  • Login or register to post comments
Fri, 06/01/2012 - 15:35
JonnyBo
Offline
Joined: 04/19/2012
Groups: None

I am trying to implement a php couchbase/ext 1.0 connection similar to what is described here (and pasted in the code block below): http://www.couchbase.com/wiki/display/couchbase/Couchbase+PHP+Client+Lib...

However, I am having trouble programmatic determining if the new Couchbase object is connected. $cb comes back as an instance of Couchbase regardless if it connected to the server or not. The only differences I see when a connection fails (E.g. server1:8091 is down) I get a PHP warning that the connection failed ( Warning: Couchbase::__construct(): Failed to connect libcouchbase to server ) and the resource handle ( _handle:Couchbase:private ) is empty. I do not know what or how to accurately check for or catch the connection error.

I have read that the addServer method may be added and/or the connection method may be updated in version 1.1 but I am not ready to use etx 1.1 at this time.

Thank you for taking the time to offer any advice.

$servers = array("http://server1:8091", "http://server2:8091");
 
do {
  if(empty($servers)) {
    echo "no suitable server found";
    // throw
  }
  $server = array_pop($servers);
  $cb = new Couchbase($server);
} while(!$cb);
 
// $cb is now usable.

Top
  • Login or register to post comments
Sun, 06/03/2012 - 23:26
ingenthr
Offline
Joined: 03/16/2010
Groups:

Hm, the trick must be in the while then. I think that was authored under the assumption that the $cb would evaluate to false if it was not connected. Let me get the author of that snippet to chime in.

We hope to have a new constructor option in the 1.0.x release timeframe as well too.

Top
  • Login or register to post comments
Mon, 06/04/2012 - 03:16
jan
Offline
Joined: 02/15/2011
Groups: None

Heya,

sorry for the confusion, I got the semantics for `new Couchbase()` wrong. To be able to loop through servers to find a healthy one with 1.0, you will need to use the procedural API:

function my_couchbase_connect($servers) {
do {
if(empty($servers)) {
echo "no suitable server found";
return false;
// throw
}
$server = array_pop($servers);
$cb = couchbase_connect($server);
} while(!$cb);
return $cb;
}

$servers = array("server1:8091", "server2:8091");

$cb = my_couchbase_connect($servers);

var_dump($cb);

I'll make sure to make this possible using the OO API as well.

Cheers
Jan
--

Top
  • Login or register to post comments
Mon, 06/04/2012 - 11:42
JonnyBo
Offline
Joined: 04/19/2012
Groups: None

Thank you for the reply!

The connection while loop is now working with the procedural API. However, I need to update my app to manage the resource number. Do you know if there is any procedural documentation (E.g. couchbase_set, couchbase_get ) outside of what is available on github ( https://github.com/couchbaselabs/php-ext-couchbase/blob/master/couchbase.c )?

Any idea when the OO API maybe updated, couple days, few months?

Thank you again for the assistance.

Blake

Top
  • Login or register to post comments
Mon, 06/04/2012 - 22:47
ingenthr
Offline
Joined: 03/16/2010
Groups:

On documentation, did you see http://www.couchbase.com/docs/couchbase-sdk-php-1.0/index.html ?

Also, it will probably be more in weeks/months for the OO API.

Top
  • Login or register to post comments
Tue, 06/05/2012 - 09:40
JonnyBo
Offline
Joined: 04/19/2012
Groups: None

Thank you for the estimated time for the OO API update, I appreciate that.

In regards to the documentation, I have seen that manual. I am interested in something related to the procedural API. E.g. couchbase_connect(), couchbase_set(), couchbase_get(). I understand it may not be available at this time but any recommendation into the source or where else to look is appreciated.

I have an additional technical question, is it possible to pass a handle/resource to the Couchbase object? E.g. start a connection using couchbase_connect and passing its result to new Couchbase ?

Thank you again for your time,
Blake

Top
  • Login or register to post comments
Wed, 06/06/2012 - 04:32
jan
Offline
Joined: 02/15/2011
Groups: None

Hi Jonny,

the procedural and OO APIs are mirrored, so couchbase_set() == Couchbase::set(), couchbase_get() == Couchbase::set() etc. the documentation and semantics are exactly the same, the only difference is that for the procedural API, the first argument to the functions is a valid Couchbase handle that you get from couchbase_connect() and for the OO API, you need an instance of the class Couchbase that you get with new Couchbase().

The API mirrors ext/memcached which is documented at http://php.net/memcached with the difference documented at http://www.couchbase.com/docs/couchbase-sdk-php-1.0/migrate-to-couchbase...

For the other question, you can't pass a couchbase_connect() handle to new Couchbase. We're not there yet, but the plan is that you can do everything with both APIs and you just pick based on the style you prefer.

I hope that helps :)

Cheers
Jan
--

Top
  • Login or register to post comments
Wed, 06/06/2012 - 11:18
JonnyBo
Offline
Joined: 04/19/2012
Groups: None

Thank you very much Jan,

This will absolutely help me get going.

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker