Skip to content
Simon Baslé edited this page Jun 22, 2015 · 1 revision

Spring Data Couchbase 2.0

This version is using the generation 2.x of the Java Couchbase SDK.

The SDK entry points and configuration

You can either use the JavaConfig method by extending the org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration class, or describe your beans as XML.

In the 2.x SDK, there are three classes that form up the base of the API and allow resource sharing (from the most shared to the less shared): the CouchbaseEnvironment allows you to tune most of the parameters for the SDK and should be reused as often as possible, the Cluster references the couchbase cluster (nodes, services, etc...) and the Bucket references a single bucket, offering all the KV/Query/... APIs. All these are thread safe and should be reused as is possible.

Creating an environment bean

To construct an environment, you can either declare one as a separate bean (by default the couchbaseEnv bean is expected when no bean name is given), or create one entirely inline inside a cluster bean definition.

Most tuning parameters in the CouchbaseEnvironment are available. For a complete list of supported attributes, see org.springframework.data.couchbase.config.CouchbaseEnvironmentParser's javadoc.

<couchbase:env id="couchbaseEnv2" connectTimeout="20000" computationPoolSize="10" />
<couchbase:cluster id="clusterWithEnvInline">
  <couchbase:env keepAliveInterval="20000"/>
</couchbase:cluster>

Note: providing no id to a couchbase:env bean definition will consider it to be the default couchbaseEnv bean.

Creating a cluster bean

The Cluster is constructed from an environment and a list of nodes to bootstrap from. The environment can be provided either inline (<couchbase:env> tag, see above) or as a reference using the env-ref attribute.

Having both inline and reference versions will only consider the inline value. Having none of the two will default to a reference to the couchbaseEnv bean (that must in this case be declared in your configuration).

Nodes are provided as a sequence of <node> tags with the IP or hostname as content.

<couchbase:cluster env-ref="someEnv">
    <!-- the following will take precedence over the someEnv reference -->
    <couchbase:env connectTimeout="2000" />
    <node>192.168.1.1</node>
    <node>192.168.1.2</node>
</couchbase:cluster>

Note: providing no id to a couchbase:cluster bean definition will consider it to be the default couchbaseCluster bean.

Creating a bucket bean

The Bucket is opened from a Cluster given its name and optionally password. It needs a couchbase:cluster bean reference (either via cluster-ref attribute or by defaulting to couchbaseCluster).

You can specify the bucket name and password to use via bucketName and bucketPassword attributes (defaults are "default" and "").

<couchbase:bucket id="bucketTest" cluster-ref="customCluste" bucketName="test" bucketPassword="p4ss" />

Note: providing no id to a couchbase:bucket bean definition will consider it to be the default couchbaseBucket bean.