Connecting to couchbase through JAVA

Hi Team,

Needed your help in opening connection with couchbase one time using Java.
and use every where in my application. Kind of opening connection one time and using every where.
Just similar to datasource connection using for MF DB2.

Appreciate your help if you provide example with Spring Boot.

below is already I have coded in configuration.

Have few questions on below.
1.) Didn’t mentioned anything about connecting cluster below.

Cluster cluster = CouchbaseCluster.create(env, "localhost" );

2.) Didn’t mentioned open bucket statement as well.

Bucket secureBucket = cluster.openBucket(“user id”, “psw”);

In which piece of below code above two things will happen.?
Did completely missed above two operations in below code. Please confirm.

@EnableCouchbaseRepositories(basePackages = “org.aexp.springbootcouchbase.mvc”)
public class CouchbaseConfig extends AbstractCouchbaseConfiguration {

private List<String> couchbasehosts;
private String couchbaseBucketName;

private String couchbaseBucketPassword;

private Long couchbaseConnectionTimeout;

private CouchbaseEnvironment couchbaseEnvironment;   

protected String getBucketName() {
    return couchbaseBucketName;

protected String getBucketPassword() {
    return couchbaseBucketPassword;

protected CouchbaseEnvironment getEnvironment() {
    CouchbaseEnvironment couchbaseEnvironment = DefaultCouchbaseEnvironment
            .defaultMetricsLoggingConsumer(true, CouchbaseLogLevel.TRACE, LoggingConsumer.OutputFormat.JSON_PRETTY)
            .runtimeMetricsCollectorConfig(DefaultMetricsCollectorConfig.create(30, TimeUnit.SECONDS ))
            .networkLatencyMetricsCollectorConfig(DefaultLatencyMetricsCollectorConfig.create(30, TimeUnit.SECONDS ))
    return couchbaseEnvironment;

public void init(){
    couchbaseEnvironment.eventBus().get().subscribe(new Action1<CouchbaseEvent>() {
		public void call(CouchbaseEvent event) {

protected List<String> getBootstrapHosts() {
	// TODO Auto-generated method stub
	return null;


Naganjaneyulu I.

Hi @naganjaneyulu.yazali,

It happens through the couchbaseCluster & couchbaseClient methods in AbstractCouchbaseConfiguration. These methods will create the cluster and bucket instances based on the properties configured by extending the class. The spring container will instantiate all the bean definitions in configuration classes. Yes, there will be only one bean instance created.

Thanks Subhashni for explanation.

If you look at my configuration class.
No where I am using the couchbaseCluster & couchbaseClient methods.

where I have use those methods in the congifuration class it’s self or out side of the configuration class where needed.

It would be great if you can able to share some sample examples.

You don’t need to use them, the AbstractCouchbaseConfiguration has those methods and they will be invoked for bean creation. You are doing it the right way by extending the configuration class.