Announcement Announcement Module
Collapse
No announcement yet.
Repositories with multiple mongo connection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Repositories with multiple mongo connection

    I'm using spring-data-mongodb and in my xml, I have usual mongo and repositories conf like this

    Code:
    <!-- first mongo -->
    <mongo:mongo id="mongo" host="localhost" port="27017"></mongo:mongo>
    
    <mongo:db-factory id="mongoDbFactory" dbname="game" mongo-ref="mongo" />
    
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    </bean>
    
    <mongo:repositories base-package="com.example.repository" repository-impl-postfix="Impl"/>
    Everything is good so far. So now I added another mongo connection
    Code:
    <!-- first mongo -->
    <mongo:mongo id="mongo1" host="localhost" port="27017"></mongo:mongo>
    
    <mongo:db-factory id="mongoDbFactory1" dbname="game" mongo-ref="mongo1" />
    
    <bean id="mongoTemplate1" class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg name="mongoDbFactory" ref="mongoDbFactory1"/>
    </bean>
    
    <mongo:repositories base-package="com.example.repository" repository-impl-postfix="Impl"/>
    
    
    <!-- second mongo -->
    <mongo:mongo id="mongo2" host="remote.server.com" port="27017"></mongo:mongo>
    
    <mongo:db-factory id="mongoDbFactory" dbname="game" mongo-ref="mongo2" />
    
    <bean id="mongoTemplate2" class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg name="mongoDbFactory" ref="mongoDbFactory2"/>
    </bean>
    
    <!-- how do i create repositories for this mongo2? -->
    My problem now is that how do I declare another set of "repository" in xml?

  • #2
    The repositories element has a mongo-template-ref attribute which you can point to the template. Make sure you use nested include/exclude elements to select only the repository interfaces that shall use the referenced connection.

    Comment


    • #3
      Sorry for this late reply, Oliver. I've been away.

      The 'mongo-template-ref' with nested include/exclude doesn't really solve my problem as I need to have the same repository connecting to different datasources. It'll be nice if I can do something like this

      Code:
      <mongo:repositories mongo-template-ref="mongoTemplate1">
           <mongo:repository id="fictionBookRepository" interface="com.example.repository.BookRepository" />
      </mongo:repositories>
      
      <mongo:repositories mongo-template-ref="mongoTemplate2">
           <mongo:repository id="scienceBookRepository" interface="com.example.repository.BookRepository" />
      </mongo:repositories>
      So in my application I could do:
      Code:
      @AutoWired
      private BookRepository fictionBookRepository;
      
      @AutoWired
      private BookRepository scienceBookRepository;
      Last edited by joea88; Jan 30th, 2013, 12:58 AM. Reason: fixed mongo-template-ref typo

      Comment


      • #4
        Similar problem here.

        A boiled down version of our situation is provided. The actual implementation uses dozens of repositories that follow a similar pattern.


        Consider: There exists a domain project containing POJOs and repos. There exists a separate web service project with extended versions of the repositories that just overwrite repo interfaces annotations to point at different database instances.

        First consider the following interfaces:

        Code:
        package com.foo.bar.domain.repositories;
        @Repository("domainObjRepository")
        public interface DomainObjRepository extends MongoRepository<DomainObj, String> {
                /* queries go here */
        }
        and then the specialized interface which is intended to point at production

        Code:
        package com.foo.bar.ws.repositories;
        @Repository("domainObjProdRepository")
        public interface DomainObjProdRepository extends DomainObjRepository {
        }
        Finally, the app-config.xml

        Code:
            <mongo:mongo id="stagingMongo" replica-set="${staging}"/>
            <mongo:mongo id="productionMongo" replica-set="${production}" />
        
            <mongo:db-factory id="stagingDBFactory" dbname="foobar" mongo-ref="stagingMongo"/>
            <mongo:db-factory id="productionDBFactory" dbname="foobar" mongo-ref="productionMongo"/>
        
            <bean id="stagingMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
                <constructor-arg ref="stagingDBFactory"/>
            </bean>
            <bean id="productionMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
                <constructor-arg ref="productionDBFactory"/>
            </bean>
        
            
            <mongo:repositories base-package="com.foo.bar.domain.repositories" >
                <mongo:repository id="domainObjRepository" mongo-template-ref="stagingMongoTemplate"/>
            </mongo:repositories>
            
            <mongo:repositories base-package="com.foo.bar.ws.repositories" >
                <mongo:repository id="domainObjProdRepository" mongo-template-ref="productionMongoTemplate"/>
            </mongo:repositories>
        
            <jaxrs:server id="restContainer" address="/" docLocation="docs">
                <jaxrs:serviceBeans>
                    <!-- Staging -->
                    <bean id="serviceStaging" class="com.foo.bar.ws.services.DomainObjService">
                        <constructor-arg><ref bean="stagingRepo"/></constructor-arg>
                    </bean>
        
                    <!-- Production -->
                    <bean id="serviceProduction" class="com.foo.bar.ws.services.DomainObjService">
                        <constructor-arg><ref bean="productionRepo"/></constructor-arg>
                    </bean>
                </jaxrs:serviceBeans>
            </jaxrs:server>

        The above configuration and interfaces allow the instancing of two of the same type of service with accept an injected Repository of type DomainObjRepository. One service is used for staging, the other for production.

        When trying to upgrade to 1.1.0, this configuration isn't possible, as the repositories element no longer accepts a sequence of repository elements.


        My alternative, so far as I can tell, is to create implementations of each repository, and manually instantiate them in the web service.

        I'd much prefer to have Repository Sequence back, as that's just way more extra code.

        Are there other solutions that I haven't considered? Recommendations, best practices, etc?

        Regards,
        KG
        Last edited by kgargano; Jan 30th, 2013, 08:56 AM. Reason: typo2

        Comment

        Working...
        X