Android Learner
Android Learner

Reputation: 2579

How to set database name at runtime in Spring Hibernate

Problem description

I have 6 databases from 6 different machines, and having one cloud server. It will decided at the time of user login that which database is going to use in application.

I am able to connect with single database. But I have no idea to handle dynamic names of databases.

Update:

I am able to change databases dynamically, but one problem is going on. There are some stored procedures in only one database that is common (placed at cloud server), (I have restriction where I can't add stored procedures to all databases), so I am not able to call stored procedures for any other databases. How can I call?

Please give me some ideas.

Upvotes: 0

Views: 4428

Answers (2)

Jesse Webb
Jesse Webb

Reputation: 45243

This Java Ranch Forum Topic explains how to do it. It shows you how to change username/password at runtime but I imagine that it would be the same pattern to change parts of the connection string or anything else.

http://www.coderanch.com/t/489984/ORM/java/modify-hibernate-cfg-xml-at

Upvotes: 1

RP-
RP-

Reputation: 5837

In Spring there is a class called AbstractRoutingDataSource which will suites for your requirement, Go through the documentation you will find some help on how to implement a concrete class of this, You need to change(or add) some part of you existing code in order to configure the dynamic Data source. This blog from spring source is explaining this in action.

Upvotes: 3

Related Questions