When developping a multitenant Java web application with Hibernate 4.1.12 we found two configuration modes that seem to work, one with "hibernate.multiTenancy", one without.
The
hibernate.cfg.xml looks like:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Standard configuration -->
<property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.connection.autocommit">true</property><!-- legacy non-transactional DB -->
<!-- HERE LIES THE MULTITENANCY CONFIGURATION -->
<!-- Persistent classes -->
<mapping class="com.foo.model.Bar1"/>
<mapping class="com.foo.model.Bar2"/>
<!-- Other entities here -->
</session-factory>
</hibernate-configuration>
Multitenancy configuration
with "hibernate.multiTenancy" is:
Code:
<!-- Multitenancy configuration (with Hibernate multitenancy support) -->
<property name="hibernate.multiTenancy">DATABASE</property>
<property name="hibernate.tenant_identifier_resolver">com.foo.hibernate.TenantResolverImpl</property>
<property name="hibernate.multi_tenant_connection_provider">com.foo.hibernate.MultiTenantConnectionProviderImpl</property>
<property name="hibernate.current_session_context_class">com.foo.hibernate.CurrentSessionContextImpl</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
Multitenancy configuration
without "hibernate.multiTenancy" is:
Code:
<!-- Multitenancy configuration (without Hibernate multitenancy suport, only home made multitenancy) -->
<property name="hibernate.connection.provider_class">com.foo.hibernate.ConnectionProviderImpl</property>
<property name="hibernate.current_session_context_class">com.foo.hibernate.CurrentSessionContextImpl</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
Some implementation details:
TenantResolverImpl returns the name of the current tenant, obtained from a thread local of the application.
MultiTenantConnectionProviderImpl and ConnectionProviderImpl return a connection from a JDBC datasource whose JNDI name is built from the tenant name (e.g. String datasourceName = "java:comp/env/" + tenantName "_DS").
CurrentSessionContextImpl stores multiple session in a ThreadLocal<String, Session> where the map key is the tenant name (we do not use Hibernate "current session" implementations because of what I described in https://forum.hibernate.org/viewtopic.php?f=1&t=1035802&p=2480115 ).
So far both have been working fine.
The question is: what are the differences between these two modes? Are we missing something?Thank you for your time.