Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-data-mongodb connect to multiple databases in one Mongo instance

I am using the latest spring-data-mongodb (1.1.0.M2) and the latest Mongo Driver (2.9.0-RC1). I have a situation where I have multiple clients connecting to my application and I want to give each one their own "schema/database" in the same Mongo server. This is not a very difficult task to achieve if I was using the driver directly:

Mongo mongo = new Mongo( new DBAddress( "localhost", 127017 ) );  DB client1DB = mongo.getDB( "client1" ); DBCollection client1TTestCollection = client1DB.getCollection( "test" ); long client1TestCollectionCount = client1TTestCollection.count();  DB client2DB = mongo.getDB( "client2" ); DBCollection client2TTestCollection = client2DB.getCollection( "test" ); long client2TestCollectionCount = client2TTestCollection.count(); 

See, easy. But spring-data-mongodb does not allow an easy way to use multiple databases. The preferred way of setting up a connection to Mongo is to extend the AbstractMongoConfiguration class:

You will see that you override the following method:

getDatabaseName() 

So it forces you to use one database name. The repository interfaces that you then build use that database name inside the MongoTemplate that is passed into the SimpleMongoRepository class.

Where on earth would I stick multiple database names? I have to make multiple database names, multiple MongoTempates (one per database name), and multiple other config classes. And that still doesn't get my repository interfaces to use the correct template. If anyone has tried such a thing let me know. If I figure it out I will post the answer here.

Thanks.

like image 333
sbzoom Avatar asked Aug 22 '12 17:08

sbzoom


People also ask

Can we connect to 2 different database from spring boot?

Spring boot allows you to connect to multiple databases by configuring multiple data sources in a single spring boot application using hibernate and JPA. Spring boot enables repositories to connect to multiple databases using JPA from a single application.

Can MongoDB have multiple databases?

mongoDB database : Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.

What is the difference between MongoOperations and MongoTemplate?

MongoTemplate provides a simple way for you to save, update, and delete your domain objects and map those objects to documents stored in MongoDB. You can save, update and delete the object as shown below. MongoOperations is the interface that MongoTemplate implements.


1 Answers

Here is a link to an article I think is what you are looking for http://michaelbarnesjr.wordpress.com/2012/01/19/spring-data-mongo/

The key is to provide multiple templates

configure a template for each database.

<bean id="vehicleTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">     <constructor-arg ref="mongoConnection"/>     <constructor-arg name="databaseName" value="vehicledatabase"/> </bean> 

configure a template for each database.

<bean id="imageTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">         <constructor-arg ref="mongoConnection"/>         <constructor-arg name="databaseName" value="imagedatabase"/> </bean>  <bean id="vehicleTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">     <constructor-arg ref="mongoConnection"/>     <constructor-arg name="databaseName" value="vehicledatabase"/> </bean> 

Now, you need to tell Spring where your repositories are so it can inject them. They must all be in the same directory. I tried to have them in different sub-directories, and it did not work correctly. So they are all in the repository directory.

<mongo:repositories base-package="my.package.repository">     <mongo:repository id="imageRepository" mongo-template-ref="imageTemplate"/>     <mongo:repository id="carRepository" mongo-template-ref="vehicleTemplate"/>     <mongo:repository id="truckRepository" mongo-template-ref="vehicleTemplate"/> </mongo:repositories> 

Each repository is an Interface and is written as follows (yes, you can leave them blank):

@Repository public interface ImageRepository extends MongoRepository<Image, String> {  }  @Repository public interface TruckRepository extends MongoRepository<Truck, String> {  } 

The name of the private variable imageRepository is the collection! Image.java will be saved to the image collection within the imagedb database.

Here is how you can find, insert, and delete records:

@Service public class ImageService {      @Autowired     private ImageRepository imageRepository; } 

By Autowiring you match the variable name to the name (id) in your configuration.

like image 56
john Avatar answered Sep 20 '22 17:09

john