Results 1 to 2 of 2

Thread: SpringData:mongoDb Multiple DB Connections with Repository Pattern

  1. #1
    Join Date
    May 2012
    Posts
    1

    Default 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 at 09:48 PM.

  2. #2
    Join Date
    Feb 2005
    Location
    Wilmington, DE
    Posts
    11

    Default

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •