-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Multitenancy with "hibernate.multiTenancy" or not?
PostPosted: Mon Jul 28, 2014 9:53 am 
Newbie

Joined: Mon Jul 28, 2014 9:18 am
Posts: 6
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.


Top
 Profile  
 
 Post subject: Re: Multitenancy with "hibernate.multiTenancy" or not?
PostPosted: Wed Apr 29, 2015 7:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It is better to use the explicit form with hibernate.multiTenancy which does a few additional sanity checks. But what you do explicitly is mostly what the hibernate.multiTenancy is about.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Multitenancy with "hibernate.multiTenancy" or not?
PostPosted: Wed Apr 29, 2015 8:21 am 
Newbie

Joined: Mon Jul 28, 2014 9:18 am
Posts: 6
Ok, thank you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.