How to switch database dynamically at runtime


#1

I want to create an application with Java application with Hibernate with multiple databases on runtime.

Actually i am creating multitenant application with following flow:

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

2- Major application ( Java + hibernate ) will now loaded start pointing to that newly created database which contains users and other stuff data.

3- I want to add oauth2 rest security as well. so the configuration should be acordingly.

now the question is that how to achieve this in Hibernate application and swtich the database configuration on runtime?


#2

This is not a question about Hibernate, but about Spring. Therefore, you need to ask it on the Spring forum.


#3

you can see now, it is updated. it is actually hiberante question.


#4

As long as you inject the right DataSource, it will work from a Hibernate perspective. How you configure the DataSource dynamically with Spring, it’s a question for the Spring forum.


#5

if i dont have datasource on initilization of application time and i want to decide the database while logging in the user and database is not already configured in and dialect and credetinals are not already define anywhere in he application, how would we be abble to switch on runtime?


#6

Hibernate supports two types of multitenancy architecture:

You can use either one based on the underlying RDBMS capabilities.

So, if the catalogs/schemas are created dynamically, assuming all tenants use the same entity mappings, you could just use a DataSource proxy to establish the JDBC Connection based on the current tenant identifier by redirecting the Connection acquisition request to the right catalog/schema.