# How to switch database dynamically at runtime

**URL:** https://discourse.hibernate.org/t/how-to-switch-database-dynamically-at-runtime/1700
**Category:** Hibernate ORM
**Created:** [November 14, 2018, 12:06pm UTC](https://discourse.hibernate.org/t/how-to-switch-database-dynamically-at-runtime/1700 "2018-11-14T12:06:04Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Irfan\_Nasim](https://avatars.discourse-cdn.com/v4/letter/i/aeb1de/32.png) [@Irfan\_Nasim](https://discourse.hibernate.org/u/Irfan_Nasim)
#### Post date: [November 14, 2018, 12:06pm UTC](https://discourse.hibernate.org/t/how-to-switch-database-dynamically-at-runtime/1700/1 "2018-11-14T12:06:04Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [November 14, 2018, 12:27pm UTC](https://discourse.hibernate.org/t/how-to-switch-database-dynamically-at-runtime/1700/2 "2018-11-14T12:27:18Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Irfan\_Nasim](https://avatars.discourse-cdn.com/v4/letter/i/aeb1de/32.png) [@Irfan\_Nasim](https://discourse.hibernate.org/u/Irfan_Nasim)
#### Post date: [November 14, 2018, 12:42pm UTC](https://discourse.hibernate.org/t/how-to-switch-database-dynamically-at-runtime/1700/3 "2018-11-14T12:42:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [November 14, 2018, 1:01pm UTC](https://discourse.hibernate.org/t/how-to-switch-database-dynamically-at-runtime/1700/4 "2018-11-14T13:01:30Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Irfan\_Nasim](https://avatars.discourse-cdn.com/v4/letter/i/aeb1de/32.png) [@Irfan\_Nasim](https://discourse.hibernate.org/u/Irfan_Nasim)
#### Post date: [November 15, 2018, 4:35am UTC](https://discourse.hibernate.org/t/how-to-switch-database-dynamically-at-runtime/1700/5 "2018-11-15T04:35:34Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [November 15, 2018, 6:26am UTC](https://discourse.hibernate.org/t/how-to-switch-database-dynamically-at-runtime/1700/6 "2018-11-15T06:26:52Z")

</div>

Hibernate supports two types of multitenancy architecture:

- [catalog-level multitenancy](https://vladmihalcea.com/hibernate-database-catalog-multitenancy/)
- [schema-level multitenancy](https://vladmihalcea.com/hibernate-database-schema-multitenancy/)

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.
