Announcement Announcement Module
Collapse
No announcement yet.
Couchbase - Connecting to multiple buckets Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Couchbase - Connecting to multiple buckets

    I'm new to Spring & Spring-Data-Couchbase so I suspect I'm doing something stupid, but here goes. I'm trying to get spring couchbase configured so that I can have two separate CouchbaseClients, one connected to a "session" bucket and another connected to a "main" bucket.

    This is via javaConfig so I've got the following @Configuration classes. I've tried to setup shared properties in a base class inheriting from AbstractCouchbaseConfiguration and then have a subclass of that for each client connection I want to create, so I can name the beans appropriately. The subclasses are static inners at the moment, for organization only (bad idea?).

    Code:
    @Configuration
    public abstract class CouchbaseConfig extends AbstractCouchbaseConfiguration {
        private final static Log log = LogFactory.getLog(CouchbaseConfig.class);
    
        public static final String MAIN_BUCKET_CLIENT = "mainCouchbaseClient";
        public static final String MAIN_BUCKET_TEMPLATE = "mainCouchbaseTemplate";
        public static final String SESSION_BUCKET_CLIENT = "sessionCouchbaseClient";
        public static final String SESSION_BUCKET_TEMPLATE = "sessionCouchbaseTemplate";
    
        @Value("${couchbase.bootstrapHosts}")
        protected String hosts;
    
        @Value("${myappDeploymentName}_${myappDeploymentCo uchbaseSessionBucket}")
        protected String sessionBucket;
    
        @Value("${myappDeploymentCouchbaseSessionBucketPW} ")
        protected String sessionBucketPassword;
    
        @Value("${myappDeploymentName}_${myappDeploymentCo uchbaseMainBucket}")
        protected String mainBucket;
    
        @Value("${myappDeploymentCouchbaseMainBucketPW}")
        protected String mainBucketPassword;
    
        @Configuration
        @EnableCouchbaseRepositories(
            basePackages = {"myapp.server.repositories.main"},
            couchbaseTemplateRef = MAIN_BUCKET_TEMPLATE
        )
        public static class MainBucketConfig extends CouchbaseConfig {
            @Override
            protected List<String> bootstrapHosts() {
                return Arrays.asList(hosts.split("[, ]"));
            }
    
            @Override
            protected String getBucketName() {
                return mainBucket;
            }
    
            @Override
            protected String getBucketPassword() {
                return mainBucketPassword;
            }
    
            // Override so we can name it as the "main" bucket client
            @Override
            @Bean(destroyMethod = "shutdown", name = MAIN_BUCKET_CLIENT)
            public CouchbaseClient couchbaseClient() throws Exception {
                return super.couchbaseClient();
            }
    
            // Override so we can name it as the "main" bucket template
            @Bean(name = MAIN_BUCKET_TEMPLATE)
            public CouchbaseTemplate couchbaseTemplate() throws Exception {
                return new CouchbaseTemplate(couchbaseClient(), mappingCouchbaseConverter(), translationService());
            }
    
        }
    
        @Configuration
        @EnableCouchbaseRepositories(
            basePackages = {"myapp.server.repositories.session"},
            couchbaseTemplateRef = SESSION_BUCKET_TEMPLATE
        )
        public static class SessionBucketConfig extends CouchbaseConfig {
        @Override
        protected List<String> bootstrapHosts() {
            return Arrays.asList(hosts.split("[, ]"));
        }
    
        @Override
        protected String getBucketName() {
            return sessionBucket;
        }
    
        @Override
        protected String getBucketPassword() {
            return sessionBucketPassword;
        }
    
        // Override so we can name it for the session bucket
        @Override
        @Bean(destroyMethod = "shutdown", name = SESSION_BUCKET_CLIENT)
        public CouchbaseClient couchbaseClient() throws Exception {
            return super.couchbaseClient();
        }
    
        // Override so we can name it for the session bucket
        @Bean(name = SESSION_BUCKET_TEMPLATE)
        public CouchbaseTemplate couchbaseTemplate() throws Exception {
            return new CouchbaseTemplate(couchbaseClient(), mappingCouchbaseConverter(), translationService());
        }
    
        }
    }
    When I run this code (it's a spring boot app) I get the following exception

    Code:
     Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainCouchbaseTemplate' defined in class path resource [org/fgsake/omgyes/server/config/CouchbaseConfig$MainBucketConfig.class]:
    nstantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException:
    Factory method [public org.springframework.data.couchbase.core.CouchbaseTemplate org.fgsake.omgyes.server.config.CouchbaseConfig$MainBucketConfig.couchbaseTemplate() throws java.lang.Exception] threw exception;
    nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'couchbaseMappingContext' is defined
    ...
    Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.couchbase.core.CouchbaseTemplate org.fgsake.omgyes.server.config.CouchbaseConfig$MainBucketConfig.couchbaseTemplate() throws java.lang.Exception] threw exception;
    nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'couchbaseMappingContext' is defined
    Should I override all of the @Bean methods in the AbstractCouchbaseConfiguration in each subclass? I suspect I'm going about this all wrong... any help would be much appreciated!

  • #2
    Answering my own question: just split the two into separate top-level @Configuration classes and deal with them each individually. Naming the CouchbaseClient and CouchbaseTemplate beans for each does let you associate the client with the template, as desired, and everything works fine.

    Comment


    • #3
      Please, move your question to the StackOverflow - we are going to close this forum soon and rely on SO.
      We need to clean this forum (old unanswered question) before close it.

      Thanks for understanding

      Comment

      Working...
      X