# Multischema support for Hibernate 5.X

**URL:** https://discourse.hibernate.org/t/multischema-support-for-hibernate-5-x/1285
**Category:** Hibernate ORM
**Created:** [August 27, 2018, 2:12pm UTC](https://discourse.hibernate.org/t/multischema-support-for-hibernate-5-x/1285 "2018-08-27T14:12:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![zeigler](https://avatars.discourse-cdn.com/v4/letter/z/90ced4/32.png) [@zeigler](https://discourse.hibernate.org/u/zeigler)
#### Post date: [August 27, 2018, 2:12pm UTC](https://discourse.hibernate.org/t/multischema-support-for-hibernate-5-x/1285/1 "2018-08-27T14:12:11Z")

</div>

I currently have multitenant support implemented using CurrentTenantIdentifierResolver and AbstractDataSourceBasedMultiTenantConnectionProviderImpl. Things are working well to this point. Currently I am working on a cron job that runs during the night to clean up data from the tables. I am having trouble getting this to work.

I first query my database to dynamically determine the current list of tenant schemas. This can grow and shrink dynamically. Next, I would like to iterate over each of these schemas, query a couple of tables in it, and delete rows based upon what I find. I am having trouble switching the schema in my query. Anything I try is failing since the MultiTenantConnectionProvider is overriding the tenant with what it thinks based upon tenant ID. Since this is a cron job there is no tenant ID and it only querys my “default” database every time.

I attempted to use the Work API and set the schema in the execute() method. I don’t understand where this connection comes from since it is not obtained via getConnection() in my MultiTenantConnectionProvider. So this is not working either.

Is there a solution for iterating over all my schemas and performing work like I am describing?

---

<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: [August 27, 2018, 3:08pm UTC](https://discourse.hibernate.org/t/multischema-support-for-hibernate-5-x/1285/2 "2018-08-27T15:08:43Z")

</div>

The `CurrentTenantIdentifierResolver` is used when the `Session` or `EntityManager` is created to resolve the tenant identifier. Afterward, the same tenant identifier will be used for the whole duration of that particular Persistence Context.

What you need to do is figure out the tenant identifier dynamically prior to creating a Hibernate `Session` and then use that to pass it to the next `Session` via `ThreadLocal` and `CurrentTenantIdentifierResolver` or via the `SessionBuilder#tenantIdentifier`.

So, you need to change the `CurrentTenantIdentifierResolver` to be able to take the `tenantIdentifier` dynamically, and also make sure you create a new `Session` for each new tenant.

---

<div class="post-metadata">

### Author: ![zeigler](https://avatars.discourse-cdn.com/v4/letter/z/90ced4/32.png) [@zeigler](https://discourse.hibernate.org/u/zeigler)
#### Post date: [August 27, 2018, 5:09pm UTC](https://discourse.hibernate.org/t/multischema-support-for-hibernate-5-x/1285/3 "2018-08-27T17:09:26Z")

</div>

Thank you for the prompt reply. I will give this a try.
