DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Enhanced Query Caching Mechanism in Hibernate 6.3.0
  • Architecting for <1s Latency: Managing Eventual Consistency in Distributed Search Platforms
  • API Testing Frameworks: How to Pick the Right One and Actually Use It Well
  • Building a Reusable Framework to Standardize API Ingestion in an On-Prem Lakehouse

Trending

  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • AI Agents and Frontier Models: Insights From The Neuron
  • Why Kotlin or Java Is the Wrong Question For Enterprises

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Multi-Tenancy and Its Improved Support in Hibernate 6.3.0

Multi-Tenancy and Its Improved Support in Hibernate 6.3.0

Multi-tenancy and a comparison of its traditional implementation with enhanced support in Hibernate 6.3.0, which was released in December 2024.

By 
Karthik Kamarapu
·
Kali Rama Krishna Vucha
·
Jan. 24, 25 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
11.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Multi-tenancy has become an important feature for modern enterprise applications that need to serve multiple clients (tenants) from a single application instance. While an earlier version of Hibernate had support for multi-tenancy, its implementation required significant manual configuration and custom strategies to handle tenant isolation, which resulted in higher complexity and slower processes, especially for applications with a number of tenants.

The latest version of Hibernate 6.3.0, which was released on December 15, 2024, addressed the above limitations with enhanced multi-tenancy support through better tools for tenant identification, schema resolution, and enhanced performance for handling tenant-specific operations. This article talks about how Hibernate 6.3.0 enhanced the traditional multi-tenancy implementation significantly.

Traditional Multi-Tenancy Implementation

Before Hibernate 6.3.0 was released, multi-tenancy required developers to set up tenant strategies manually. For example, the developers needed to implement some custom logic for schema or database resolution  and use the the Hibernate-provided CurrentTenantIdentifierResolver interface to identify the current tenant, which was not only error-prone but also added significant operational complexity and performance overhead.

Below is an example of how multi-tenancy was configurated traditionally:

Java
 
public class CurrentTenantIdentifierResolverImpl implements CurrentTenantIdentifierResolver {
    @Override
    public String resolveCurrentTenantIdentifier() {
        return TenantContext.getCurrentTenant(); // Custom logic for tenant resolution
    }

    @Override
    public boolean validateExistingCurrentSessions() {
        return true;
    }
}

SessionFactory sessionFactory = new Configuration()
    .setProperty("hibernate.multiTenancy", "SCHEMA")
    .setProperty("hibernate.tenant_identifier_resolver", CurrentTenantIdentifierResolverImpl.class.getName())
    .buildSessionFactory();


Output:

VB.NET
 
INFO: Resolving tenant identifier
INFO: Current tenant resolved to: tenant_1
INFO: Setting schema for tenant: tenant_1


Improved Multi-Tenancy in Hibernate 6.3.0

Hibernate 6.3.0 added significant improvements to simplify and enhance multi-tenancy management, and the framework now offers:

1. Configurable Tenant Strategies

Developers can use built-in strategies or extend them to meet any specific application needs. For example, a schema-based multi-tenancy strategy can be implemented without any excessive boilerplate code.

Example of the new configuration:

Java
 
@Configuration
public class HibernateConfig {

    @Bean
    public MultiTenantConnectionProvider multiTenantConnectionProvider() {
        return new SchemaBasedMultiTenantConnectionProvider(); // Built-in schema-based provider
    }

    @Bean
    public CurrentTenantIdentifierResolver tenantIdentifierResolver() {
        return new CurrentTenantIdentifierResolverImpl();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
            .dataSource(dataSource())
            .properties(hibernateProperties())
            .packages("com.example.app")
            .persistenceUnit("default")
            .build();
    }
}


Log output:

VB.NET
 
INFO: Multi-tenant connection provider initialized
INFO: Tenant resolved: tenant_2
INFO: Schema switched to: tenant_2


2. Performance Optimization

In earlier versions, switching between tenant schemas could have resulted in latencies, especially for frequent tenant-specific queries.  Hibernate 6.3.0 optimized schema switching at the database connection level, which resulted in faster query execution and improved performance in multi-tenant environments. 

Example output:

VB.NET
 
DEBUG: Connection switched to tenant schema: tenant_3
DEBUG: Query executed in 15ms on schema: tenant_3


3. Improved API Support

Hibernate 6.3.0 introduces new APIs that allow developers to manage tenant-specific sessions and transactions more effectively. For example, developers can programmatically switch tenants within a session using short API calls.

Java
 
Session session = sessionFactory.withOptions()
    .tenantIdentifier("tenant_4")
    .openSession();
Transaction transaction = session.beginTransaction();
// Perform tenant-specific operations
transaction.commit();
session.close();


 The above snippet makes it easy to handle multi-tenant operations dynamically, as the framework ensures proper schema management behind the scenes.

Conclusion

The improvements in Hibernate 6.3.0 address many of the existing challenges that developers faced with earlier implementations, and by simplifying tenant identification and schema resolution, the framework reduced the development effort required for scalable multi-tenancy setup. Additionally, the performance optimizations ensure that tenant-specific operations such as schema switching or query execution are faster, more reliable, and more efficient.

API Hibernate Performance Framework

Opinions expressed by DZone contributors are their own.

Related

  • Enhanced Query Caching Mechanism in Hibernate 6.3.0
  • Architecting for <1s Latency: Managing Eventual Consistency in Distributed Search Platforms
  • API Testing Frameworks: How to Pick the Right One and Actually Use It Well
  • Building a Reusable Framework to Standardize API Ingestion in an On-Prem Lakehouse

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook