• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to switch database dynamically at runtime with Spring Data JPA ORM

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create an application with Java, Spring and Spring JPA with multiple databases on run time.

Actually i am creating multi-tenant application with following flow:

1- one Application may be php: from website user will create account, the system will run the process and will create database and respective configuration.

2- Major application ( Java + Spring JPA, Spring REST) will now loaded, start pointing to that newly created database which contains users and other stuff data may be on the basis of tenant Id or some other pointing flag.

Further Plan to add 3rd point; 3- oAuth2 rest security as well. so the configuration should be accordingly.

now the question is that how to achieve this in Spring Data JPA application and switch the database configuration on run time?
 
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you switching?  Do the databases have the same kind of data?
 
Irfan Nasim
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:Why are you switching?  Do the databases have the same kind of data?



because this is my requirement and i have multiple client and want to convert application on SaaS model with every client has its own database. When client will signup the database will dynamically created and a unique tenantId will assign to him. so when user will logged in by providing tenantId on LoggedIn screen the respective database will connect with it.

Yes Every Database have the same schema.
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so the program will use a different database depending on the user.  You will obviously have to implement that service yourself and then inject it wherever you need it.  I'm not really sure how to make a new repository instance programmatically. If you are using the spring jpa interfaces then you will need to look into making those.  Probably the easiest way is to directly use JPA and have the Entitymanagers in a service that you pass around.
 
Irfan Nasim
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:Ok so the program will use a different database depending on the user.  You will obviously have to implement that service yourself and then inject it wherever you need it.  I'm not really sure how to make a new repository instance programmatically. If you are using the spring jpa interfaces then you will need to look into making those.  Probably the easiest way is to directly use JPA and have the Entitymanagers in a service that you pass around.



I have spent 10+ days to figure it out, but found no way to achieve with any ORM in java. can do by using JDBC template. if wanted to achieve by JDBC template then i must have to write own Data layer to communicate. it mean i have to refactored the whole data layer and spring jpa will be eliminate may be i have write some mini ORM. i believe i have to manage the connection pooling by myself etc etc
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can programmatically make entitymanagers.  You don't need to use jdbc.

Here's a link about custom repositories.  spring custom repo link
I would try adding a functionality to switch out the entitymanager.  You could store the user's db persistence info in a file or a database if there will be alot of configurations.
 
Irfan Nasim
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:You can programmatically make entitymanagers.  You don't need to use jdbc.

Here's a link about custom repositories.  spring custom repo link
I would try adding a functionality to switch out the entitymanager.  You could store the user's db persistence info in a file or a database if there will be alot of configurations.



do you have any git repo as POC for reference to see ?
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The link has examples of how to make custom repositories.  An example of those might be in the spring data examples.  What you are doing seems pretty unique so I doubt you will find a project with a similar setup at least publicly available.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!