Couchbase SDKs support persistent connections which enable you to send multiple requests and receive multiple responses using the same connection. How the Couchbase SDKs implement persistent connections varies by SDK. Here are the respective approaches you can use:
PHP: Persistent connections for PHP clients are actually persistent memory that we use across multiple requests in a PHP process. Typically you use one PHP process per system process. The web server that is currently in use in your system will determine this. To configure the PHP SDK to maintain a persistent connection you would use these parameters in your connection:
$cb = new Couchbase("192.168.1.200:8091", "default", "", "default", true); // uses the default bucket
This example uses the default bucket. Arguments include host:port, username, password, bucket name, and true indicates we want to use a persistent connection. For more information, refer to the Couchbase PHP SDK Language Reference.
Java: When you create connection with the Java SDK, the connection is a thread-safe object that can be shared across multiple processes. The alternative is that you can create a connection pool which contains a multiple connection objects.
For more information, see Couchbase Java SDK: Connecting to the Server.
.Net: Connections that you create with the .net SDK are also thread-safe objects; for persisted connections, you can use a connection pool which contains multiple connection objects. You should create only a single static instance of a Couchbase client per bucket, in accordance with .Net framework. The persistent client will maintain connection pools per server node. For more information, see MSDN: AppDomain Class
You can persist a Couchbase client storing it in a way such
that the Ruby garbage collector does not remove from memory.
To do this, you can create a singleton object that contains
the client instance and the connection information. You
should access the class-level method,
Couchbase.bucket instead of
Couchbase.connect to get the client
instance.
When you use Couchbase.bucket it
will create a new client object when you first call it and
then store the object in thread storage. If the thread is
still alive when the next request is made to the ruby
process, the SDK will not create a new client instance, but
rather use the existing one:
# Simple example to connect using thread local singleton Couchbase.connection_options = { :bucket => "my", :hostname => "example.com", :password => "secret" } # this call will user connection_options to initialize new connection. # By default Couchbase.connection_options can be empty Couchbase.bucket.set("foo", "bar") # Amend the options of the singleton connection in run-time Couchbase.bucket.reconnect(:bucket => "another")
The first example demonstrates how you can create a client
instance as a singleton object, the second one will use the
class-level Couchbase.bucket
constructor to create a persistent connection. The last
example demonstrates how you can update the properties of
the singleton connection if you reconnect.
For more information about persistent connections for an SDK, see the individual Language Reference for your chosen SDK.