Connection management

By now, you have understood how to connect to the bucket and perform various operations using the Java SDK. We can conclude that there are two ways of connecting to the bucket:

  • Synchronously:
    Cluster cluster = CouchbaseCluster.create();
    Bucket bucket = cluster.openBucket();
  • Asynchronously:
    AsyncBucket asyncBucket = bucket.async();

    Tip

    It's best practice to create one instance of CouchbaseCluster per application.

Couchbase SDKs based on Java and .Net are designed to be thread-safe for each operation. There is a class called CouchbaseEnvironment that can be used to customize connection to the cluster.

How we create this environment is given in the following code:

CouchbaseEnvironment clusterEnv = DefaultCouchbaseEnvironment .builder() ...

Get Learning Couchbase now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.