Announcement Announcement Module
Collapse
No announcement yet.
Multiple databases with one Mongo connection in spring-data-mongodb Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple databases with one Mongo connection in spring-data-mongodb

    Hello.
    I am trying to use spring-data-mongodb to connect to multiple databases within one Mongo server. This does not seem possible because the Repository interfaces use only one MongoTemplate, which takes only one databaseName, which is configured at startup (in AbstractMongoConfiguration). Does anyone know a way to do this?

    Here is the same question posted to StackOverflow:

    http://stackoverflow.com/questions/1...mongo-instance

    Thanks.

  • #2
    Is this something that could help you?

    Originally posted by sbzoom View Post
    Hello.
    I am trying to use spring-data-mongodb to connect to multiple databases within one Mongo server. This does not seem possible because the Repository interfaces use only one MongoTemplate, which takes only one databaseName, which is configured at startup (in AbstractMongoConfiguration). Does anyone know a way to do this?

    Here is the same question posted to StackOverflow:

    http://stackoverflow.com/questions/1...mongo-instance

    Thanks.
    This configuration example works:

    Code:
    <context:property-placeholder location="classpath:mongo.properties"/>
    
        <mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}">
            <mongo:options
                    connections-per-host="${mongo.connectionsPerHost}"
                    threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
                    connect-timeout="${mongo.connectTimeout}"
                    max-wait-time="${mongo.maxWaitTime}"
                    auto-connect-retry="${mongo.autoConnectRetry}"
                    socket-keep-alive="${mongo.socketKeepAlive}"
                    socket-timeout="${mongo.socketTimeout}"
                    slave-ok="${mongo.slaveOk}"
                    write-number="1"
                    write-timeout="0"
                    write-fsync="true"/>
        </mongo:mongo>
    
        <mongo:db-factory id="mongoDbFactory1" mongo-ref="mongo" dbname="domain"/>
    
        <mongo:db-factory id="mongoDbFactory2" mongo-ref="mongo" dbname="storage"/>
    
        <mongo:db-factory id="mongoDbFactory3" mongo-ref="mongo" dbname="another"/>
    
        <mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory2" />
    
        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg ref="mongoDbFactory1"/>
        </bean>
    
        <bean id="gridTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
            <constructor-arg ref="mongoDbFactory2" />
            <constructor-arg ref="converter" />
        </bean>
    
        <bean id="anotherMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg ref="mongoDbFactory3"/>
        </bean>

    Comment


    • #3
      Originally posted by rigarcia View Post
      This configuration example works:

      Code:
      <context:property-placeholder location="classpath:mongo.properties"/>
      
          <mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}">
              <mongo:options
                      connections-per-host="${mongo.connectionsPerHost}"
                      threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
                      connect-timeout="${mongo.connectTimeout}"
                      max-wait-time="${mongo.maxWaitTime}"
                      auto-connect-retry="${mongo.autoConnectRetry}"
                      socket-keep-alive="${mongo.socketKeepAlive}"
                      socket-timeout="${mongo.socketTimeout}"
                      slave-ok="${mongo.slaveOk}"
                      write-number="1"
                      write-timeout="0"
                      write-fsync="true"/>
          </mongo:mongo>
      
          <mongo:db-factory id="mongoDbFactory1" mongo-ref="mongo" dbname="domain"/>
      
          <mongo:db-factory id="mongoDbFactory2" mongo-ref="mongo" dbname="storage"/>
      
          <mongo:db-factory id="mongoDbFactory3" mongo-ref="mongo" dbname="another"/>
      
          <mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory2" />
      
          <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
              <constructor-arg ref="mongoDbFactory1"/>
          </bean>
      
          <bean id="gridTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
              <constructor-arg ref="mongoDbFactory2" />
              <constructor-arg ref="converter" />
          </bean>
      
          <bean id="anotherMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
              <constructor-arg ref="mongoDbFactory3"/>
          </bean>
      This is essentially what I have, although I have it setup in java instead of XML. I have a List of databaseNames, a List of MongoDbFactories and a Map of MongoTemplates. The next step though is getting my repository interface to use the correct mongoTemplate at runtime.

      Yes, it has to be runtime. Each client will have the same collections in their own database. I have seen documentation about how I can specify a particular MongoTemplate for each Repository - but I want to do it at runtime.

      Thanks.


      Charlie

      Comment


      • #4
        Originally posted by sbzoom View Post
        This is essentially what I have, although I have it setup in java instead of XML. I have a List of databaseNames, a List of MongoDbFactories and a Map of MongoTemplates. The next step though is getting my repository interface to use the correct mongoTemplate at runtime.

        Yes, it has to be runtime. Each client will have the same collections in their own database. I have seen documentation about how I can specify a particular MongoTemplate for each Repository - but I want to do it at runtime.

        Thanks.


        Charlie
        Sorry, I think I didn't understand well the question. I was talking about having different databases for different purpouses, not for replicas of the data. In my example you can inject any template you want to use storing the data in the database related to that template:

        Code:
            @Autowired
            MongoTemplate mongoTemplate;
        
            /*@Autowired
            MongoTemplate secondMongoTemplate;*/
        But If you want to configure replicaset maybe these links could give you an idea:
        I don't know if you are thinking in another kind of behaviour.

        Comment


        • #5
          I am not explaining correctly, sorry about that. I do not need a replica set (well, we will setup replica sets, but that is just for good practice).

          What I want is behavior in a manner like an Oracle schema. We will have one schema called "clientDB" that will have all client information and login information. Then each client will have their own schema on the server. "client1", "client2", "client3"... Each of these schemas has the same collections, the division is to keep client data separate - requirement for the project.

          MongoDB can have all these schemas/databases on the same server, which is great. We just need a way of connecting to a specific client's database when a user from that client logs in.

          The other way of doing this would be to put a "clientId" in each document - not an ideal solution for many reasons: pollutes the data and then the "clientId" would have to be added to every query.

          If I had made my own DAO classes instead of using the MongoRepository interface this would be pretty easy. I would get a MongoTemplate in each DAO and I could set the databaseName before I run each query no problem. Or, I would setup a Map of MongoTemplates and call the right one for the particular query.

          But with all of this programming being hidden behind the MongoRepository there is a lot of magic happening that is not easy to get access to. Everything is automatic and counting on having only one MongoTemplate and only one databaseName.

          Hope this helps. Thanks again for the suggestions.

          Comment


          • #6
            There are two ways to accomplish this. The first is to use non annotated dependency injection for each of your services and repositories but here is an even easier way.
            http://michaelbarnesjr.wordpress.com...ng-data-mongo/

            Comment

            Working...
            X