简体   繁体   中英

Hibernate 4.x Multi-tenancy ID generation issues with MYSQL

We are using Hibernate 4.x to achieve multi-tenancy with separate DATABASE per tenant.

We are using the increment strategy provided by Hibernate for ID generation.

The ID generation doesn't happen specific to a tenant.

Is there any way we can restrict the ID generation specific to a tenant, instead of getting from a common cache.

Another issue I have seen is on Server re-start.

After server restart, if the first SQL operation happens for Tenant 1, then Hibernate starts caching the IDs referenced from the Ids from Tenant 1 database. This can create conflicts, if the subsequent operation is performed on other Tenant and it already has the Id value provided by Hibernate cache.

I could have used Sequences for generating the IDs, but MYSQL doesn't support Sequences.

Is there any way to avoid getting the IDs from the common cache?

One of the ways to handle this issue is to use "Identity" ID generation mechanism provided by JPA. This has performance impact. But the ID generation across the tenant will be correct. I have tried this approach and it worked.

Other approaches would be to use Table based ID generation using HiLow algorithm or UUID based ID generation approach.

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