Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic database connection uisng spring

 
ujjwal kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

On my current project, iam using spring(mvc) along with hibernate and jpa. So far everything was going well.

But currently, according to clients, requirement, i need to connect database, according to the specific user. What i want to mean, every user has a different database. So, at the time of log in, i will connect every user to their respective databases.

Also i need to switch between two different databases, while user is logged in. I mean, some changes from user to the properties, will affect the master database.

Iam stuck with this requirement. Will somebody please help me, how to do this? Some sample code will be appreciated.

Thanks
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of posts on blogs out there on this subject. Unfortunately, trying to repeat the solution or give sample code is lots of work. Basically, you have to create an EntityManagerFactory or SessionFactory per database. So you would end up needing lots of them and storing them in memory somewhere like a Map. Then a Factory type pattern to find that users EMF to use from that Map.

Good Luck and remember Google is your search friend.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and I am sure this topic has been covered many times in the JPA/ORM forum here at JavaRanch. As this problem/subject isn't a Spring issue but a Hibernate/JPA issue.

So I guess, I should move this to that forum for you.

Thanks

Mark
 
ujjwal kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks mark for your reply. I will definitely look at other posts, you mentioned. But in my case, as you mentioned "you have to create an EntityManagerFactory or SessionFactory per database. So you would end up needing lots of them and storing them in memory somewhere like a Map" is going to be a problem, i think.

Because,

In my application, Super Admin can create lots of admin. At the time of admin creation, a database regarding that admin will be created. So every admin will have its personal database. As a result, i can not put/store any predefined map in memory. So in my case, what can be the solution? While every admin will browse the common login page, they will give internal information, which database they should be connected. That is the only clue, i can get.

Any help from your side will be appreciated.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic