Switching between databases Spring MongoDb - java

I have a case, where I need to switch between the mongo databases using Spring mongodata (Version: 1.6.2). Currently, I have default database configured in db-config.xml with mongo template, and have annotated repositories; Need is to switch from one db/template to another at runtime; do necessary actions and switch back to default one.
I referred to couple of links,
Spring-data-mongodb connect to multiple databases in one Mongo instance
and
Making spring-data-mongodb multi-tenant
I need to use same set of repositories at runtime. Is it possible to handle my case at configuration level? or do we need to extend Dbfactory to achieve this?
with Dbfactory, can I use same set of annotated repositories?
Appreciate any help.

You can extend:
1. `SimpleMongoDbFactory`: returning custom DB in DB `getDb(String dbName)`.
2. `MongoTemplate`: Supplying above factory.
Use appropriate MongoTemplate with the help of #Qualifier.

I once had a very similar problem.
I published the code on github, check it out multi-tenant-spring-mongodb
You basically have to extend SimpleMongoDbFactory and handle other hosts too. I just did handle multiple databases on the same server. That shouldn't be a problem.

Related

Jhipster/MongoDB custom distinct query creation (since not possible with spring repository)

I would like to create a Distinct query from Jhipster framework since it is not possible with spring repository.
My main issue is how to connect to the DB without creating a new manual connection. I am sure there must be a way to inject the mongoDB connection in my java class but I don't know how to do that.
If someone can help ?
Cheers,
Use this content for study queries in jhipster...
http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html

Spring Data Mongo - Dynamically change repository template

I have this scenario: same database structure (same collections) replicated in multiple mongo databases. This mean that I have one mongo repository for each collection (Document).
Now I need to manage these databases through the same Control Panel App, connecting to each one of them dynamically, and using the same repository classes (the databases are identical).
I know that I can specify known templates for repositories as described in this post, but this implies that I have to know the database's connection properties at startup. How can I implement a dynamic behaviour of that, instead?
The core interface you might want to look at is MongoDBFactory. You can provide a custom one by overriding mongoDbFactory() in AbstractMongoConfiguration or just an ordinary bean definition in XML.
To transparently switch between different databases, simply keep track of the one selected in the implementation and return a DB instance according to that.

Multiple Data Source using Spring JPA

I am building a stand alone java application using Spring JPA frame
work. I am able to access the DB in below scenario: if I give the
DB details in application.properties file as
spring.datasource.url=******** spring.datasource.username=******
spring.datasource.password=******
then it's working properly.
but I have to create two DB connections in the same application so,
I changes the names as below
spring.Datasource1.url=********* spring.Datasource1.username=******
spring.Datasource1.password
spring.Datasource2.url=************ spring.Datasource2.username=****
spring.Datasource2.password=*****
then it's not working.
Can you please provide the solution for it?
I have uploaded my code base in below location.
https://github.com/nagtej/MultipleDataSource
This might be helpful to you http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources
Also, to connect to multiple data sources you would need to manually configure a DataSource, EntityManagerFactory and JpaTransactionManager.
For this, you can have a look at code placed at https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources
Another good example for this is shared at http://xantorohara.blogspot.com.au/2013/11/spring-boot-jdbc-with-multiple.html

Spring Boot and Spring Data application with multiple DataSources created in runtime

I am developing a Spring Boot application that uses Spring Data JPA and will need to connect to many different databases e.g. PostreSQL, MySQL, MS-SQL, MongoDB.
I need to create all datasources in runtime i.e. user choose these data by GUI in started application:
-driver(one of the list),
-source,
-port,
-username,
-password.
And after all he writes native sql to choosen database and get results.
I read a lot of things about it in stack and spring forums(e.g. AbstractRoutingDataSource) but all of these tutorials show how to create datasources from xml configuration or static definition in java bean. It is possible to create many datsources in runtime? How to manage transactions and how to create many sessionFactories? It is possible to use #Transactional annotation? What is the best method to do this? Can someone explain me how to do this 'step by step'?
Hope it's not too late for an answer ;)
I developed a module which can be easily integrated in any spring project. It uses a meta-datasource to hold the tenant-datasource connection details.
For the tenant-datasource an AbstractRoutingDataSource is used.
Here you find my core implementation using the AbstractRoutingDataSource.
https://github.com/Dactabird/multitenancy
Here is an example to show how to integrate it. https://github.com/Dactabird/multitenancy-sample
In this example I'm using H2 embedded db. But of course you can use whatever you want.
Feel free to modify it for your purposes or to ask if questions are left!

Spring Data/JPA Repositories with DynamoDB and MySQL dual datasources

I'm familiar with how to make Spring handle multiple datasources dynamically via multiple persistence-units and multiple entityManagerFactoryBean implementations but what I'm struggling with is how to have a MySQL dialect and a DynamoDB dialect from within the same spring configuration, via spring-config xml files.
The work pattern is as follows:
[data POJO in, from some endpoint] -> Persist POJO into DynamoDB, retrieving the UUID of that object (business key as field on POJO) -> Persist UUID as a compound key (no referential integrity, it's just another column) into MySQL Database [with other related mapped entities].
I'm struggling with quite how on earth to go about adding the DynamoDB instance into the Spring configuration files to achieve this.
For what it's worth, the related repositories are going to be in separate packages.
Any starters for 10 would be gratefully received! I've done some searching but all DynamoDB mapper frameworks seem to be at a much higher level - have I missed something here? I've been looking at Spring-Data DynamoDB but still can't make the link between the configuration file and Dynamo.
Thanks in advance,
A.
========= UPDATE IN THINKING =========
I think I've gone about this the wrong way. From digging around the samples a lot more, doing a local integration test [pure dynamodb], I don't think it's possible to use DynamoDB as part of an EntityManager Factory implementation: to that end, I think I'm going to have to "create" my own repository implementations that call out to the mapper and AWS connection helper classes etc. for Dynamo, rather than using any of the JPA spring-provided code.
Unless anyone can recommend/suggest otherwise?
Question closed - after much investiation the only real way to do it is to introduce ones own interpretation of a repository and DAO-based implementation.
There is one interesting project, however, Spring Data Dynamodb. Looks interesting but not quite ready for Enterprise Production release.

Categories