VOOZH about

URL: https://dzone.com/articles/multi-tenancy-improved-support-hibernate-6-3-0

⇱ Multi-Tenancy and Its Improved Support in Hibernate 6.3.0


Related

  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.

Likes
Comment
Save
10.8K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Building a Reusable Framework to Standardize API Ingestion in an On-Prem Lakehouse
  • Beyond Django and Flask: How FastAPI Became Python's Fastest-Growing Framework for Production APIs
  • Why Front-End Performance Issues Are Commonly Back-End Issues

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: