Reputation: 1796
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 features, one without.
The hibernate.cfg.xml looks like;
<?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>
Multitenancy configuration "with Hibernate multitenancy features" is:
<!-- 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 Hibernates multitenancy features" is:
<!-- 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:
So far both have been working fine.
The question is: what are the differences between these two modes?
Thank you for your time.
Upvotes: 1
Views: 907
Reputation: 910
It is better to use the explicit form with hibernate.multiTenancy
which does a few additional sanity checks and is more future proof in case new services/components become involved in multi-tenancy in the future.
But what you do explicitly is mostly what the hibernate.multiTenancy is about. It works in your case because you have decided essentially to implement your own connection provider, disabled the JDBC metadata check and do not make use of the schema generation feature (in the "non explicitly multitenant" configuration).
Upvotes: 1