Connect Multiple Buckets inside same Couchbase cluster

Hi,

I am trying to connect to two different buckets inside same couchbase cluster.
Spring data configures the default one itself.
I have overridden all required methods in my custom bucket and mapped the repoistory to be used as well with the custom couchbase template.

  @Bean(name = "coucbaseMappingConverter")
  public MappingCouchbaseConverter mappingCustomCouchbaseConverter() throws Exception {
    MappingCouchbaseConverter converter =
        new CustomMappingCouchbaseConverter(couchbaseMappingContext());
    converter.setCustomConversions(customConversions());
    return converter;
  }

  @Bean(name = "customBucket")
  public Bucket customBucket() throws Exception {
    return couchbaseConfigurer()
        .couchbaseCluster()
        .openBucket("replicaBucket");
  }

  @Bean("customCouchbaseTemplate")
  public CouchbaseTemplate customCouchbaseTemplate() throws Exception {
    CouchbaseTemplate template =
        new CouchbaseTemplate(
            couchbaseClusterInfo(), customBucket(),
            mappingCustomCouchbaseConverter(), translationService());
    template.setDefaultConsistency(getDefaultConsistency());
    return template;
  }
  
  @SneakyThrows
  @Bean("publishRepositoryOperationMapping")
  public RepositoryOperationsMapping publishRepositoryOperationMapping(){
    RepositoryOperationsMapping baseMapping = new RepositoryOperationsMapping(customCouchbaseTemplate());
    configureRepositoryOperationsMapping(baseMapping);
    return baseMapping;
  }
  
  @Override
  @SneakyThrows
  public void configureRepositoryOperationsMapping(RepositoryOperationsMapping publishRepositoryOperationMapping) {
	  publishRepositoryOperationMapping.mapEntity(CustomRepository.class, customCouchbaseTemplate());
  }

Now In my repoistory for this custom bucket. I have the niql way to get the bucket name , #{#n1ql bucket} … this always gives me the default bucket name.

CustomRepository Interface
@Query(Select * from #{#n1ql bucket})
Optional getResponse();

in logs I can see the bucket name that gets replaced by #{#n1ql bucket} is the default bucket name and not my custom name.

Is there some more configuration required which i am missing

@Harinder_Singh I don’t do much with Spring, but here is some basic code that you can follow SDK 3.0

import java.time.Duration;
import com.couchbase.client.java.*;
import com.couchbase.client.java.json.JsonObject;
import static com.couchbase.client.java.kv.UpsertOptions.upsertOptions;

public class UpsertAndSetExpiry_v3 {

    public static void main(String... args) throws Exception {
    	Cluster cluster = Cluster.connect("192.168.3.150", "admin", "jtester");
    	
    	// the two docs will self expire in 10 minutes
    	Duration dura = Duration.ofMinutes(10);

    	// open the first bucket
    	Bucket bucket_1 = cluster.bucket("source_bkt_1");
    	Collection collection_1 = bucket_1.defaultCollection();
    	String docID_1 = "SampleDocument1";
    	try {
    		collection_1.upsert(
    			docID_1, JsonObject.create().put("a_key", "a_value"), 
    			upsertOptions().expiry(dura) );
    		System.out.println("In the first bucket, docID: " + docID_1 + " expires in " + dura.getSeconds());
    	} catch (Exception e) {
    		System.out.println("In the first bucket, upsert error for docID: " + docID_1 + " " + e);
    	} 
    	
    	// open the second bucket
    	Bucket bucket_2 = cluster.bucket("source_bkt_2");
    	Collection collection_2 = bucket_2.defaultCollection();
    	String docID_2 = "SampleDocument2";
    	try {
    		collection_2.upsert(
    			docID_2, JsonObject.create().put("a_key", "a_value"), 
    			upsertOptions().expiry(dura) );
    		System.out.println("In the second bucket, docID: " + docID_2 + " expires in " + dura.getSeconds());
    	} catch (Exception e) {
    		System.out.println("In the second bucket, upsert error for docID: " + docID_2 + " " + e);
    	}    	
    	
    	// shut it all down
        bucket_1 = null;
        bucket_2 = null;
        collection_1 = null;
        collection_2 = null;
    	cluster.disconnect(Duration.ofSeconds(2000));
    }
}

Best

Jon Strabala