Announcement Announcement Module
Collapse
No announcement yet.
SpringData:mongoDb Multiple DB Connections with Repository Pattern Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SpringData:mongoDb Multiple DB Connections with Repository Pattern

    I have been using the Repository pattern without any issues - when I fire up the server it scans the mongo:repository
    Code:
    ...
    <mongo:repositories base-package="com.mycompany.registry"/>
    ...
    element in my applicationContext and auto creates a collection for each interface extending Repository.
    Code:
    public interface AuditLogRepository extends MongoRepository<AuditLogEvent, ObjectId> 
    ...
    {
    The way I am getting my DB connection is by extending MongoTemplate and MongoDbFactory so I can get db startup properties from a properties file:
    Code:
    @Component(RegistryMongoTemplate.NAME)
    public class RegistryMongoTemplate extends MongoTemplate implements MongoOperations {
    
        public static final String NAME = "mongoTemplate";
    
        @Autowired
        public RegistryMongoTemplate(@Qualifier(RegistryDatabaseFactory.NAME) MongoDbFactory factory) {
            super(factory);
        }
    }
    
    @Component(RegistryDatabaseFactory.NAME)
    public class RegistryDatabaseFactory implements MongoDbFactory {
    
        public static final String NAME = "mongoDbFactory";
       
        private DB db = null;
    
        @Autowired
        public RegistryDatabaseFactory(
                @Qualifier(RegistryDefaultsBootstrapConfig.NAME) RegistryDefaultsBootstrapConfig bootConfig
                throws RegistryException {
       
                db = ... // use the bootstrap props to create db instance.
    
        }
        
        @Override
        public DB getDb() throws DataAccessException {
            return db;
        }
    
        ...
    }
    This all works fine. However, now I am trying to add a second connection/MongoTemplate to a different DB, but when I do, all the Repository collections are created in both DBs. I created a separate mongoTemplate, MongoDbFactory and matched them to their own mongo:repository element via mongo-template-ref attribute,
    Code:
     <mongo:repositories base-package="com.mycompany.other.stuff" mongo-template-ref="mongoTemplateOther"/>
    but I am not having any luck - all repositories are created on both DBs.
    Last edited by psuthe-01; May 9th, 2012, 10:48 PM.

  • #2
    Did you ever have any luck with this? Did you get it to work? I am trying to do something similar and would love to know.

    Thanks.

    Comment

    Working...
    X