简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM