
Originally Posted by
sbzoom
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>