2

I am trying to build multitenancy based on unique schema for each client
So there would be a single datasource to Oracle in weblogic server
the datasource would have a user which has synonyms / grants to all client schemas
Let us ignore the fact whether this is good or bad or horrible design - I just want to find a way to get it to work
I am defining the datasource as a part of my hibernate.cfg.xml file
The sessionfactory will get created from this hibernate.cfg.xml

From reading on the internet - I will have to add additional properties in my hibernate.cfg.xml

    <session-factory>
    <property name="connection.datasource">MYSQLDS</property>
    <property name="default_schema">testpage</property>
    <!-- multitenant properties start here -->
    <property name="multi_tenant_connection_provider">multiTenantConnectionProvider</property>
    <property name="multiTenancy">SCHEMA</property>
    <property name="tenant_identifier_resolver">CurrentTenantIdentifierResolverImpl</property>
    &globalpit;
</session-factory>

Now is the part that I dont understand:
It seems for CurrentTenantIdentifierResolverImpl I will implement a custom class that will implement the interface

import org.hibernate.context.spi.CurrentTenantIdentifierResolver;

public class SchemaResolver implements CurrentTenantIdentifierResolver {

@Override
public String resolveCurrentTenantIdentifier() {
  return "master"; 
}

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

So I am quite confused how this class and its methods will get invoked - I do understand that this is used to determine which schema is to be used based on some identifier unique to a client - ala user http session ?

The next part is the implementation of MultiTenantConnectionProvider and again its methods such as

public Connection getConnection(String tenantIdentifier) throws SQLException {

RANT -START This strangely starts looking like good old plain and simple JDBC :) so am wondering why I have to use hibernate :( RANT-END

So related to this method and the class - how do I consume it ?
The frustrating part is lots of places where you get to see these two classes defined - but no explanation of how these are invoked ?
Is there a complete end to end example of how to use these two classes with a clear example of how these two classes are consumed - not just the two stand alone classes please !

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
satish marathe
  • 1,073
  • 5
  • 18
  • 37
  • I tried implementing multi-tenancy with hibernate 4 but got stuck with this roadblock - https://hibernate.atlassian.net/browse/HHH-7395 – Andy Dufresne Dec 29 '14 at 09:36
  • @andy-dufresne - i think the link you are providing is for database based multi tenancy - see this link where today with help of matt deinum I got it working for schema based : http://stackoverflow.com/questions/27676480/hibernate-multitenancy-implementation-issue-with-injectservices – satish marathe Dec 29 '14 at 09:51
  • No. I do have multitenancy property set to SCHEMA. I looked into the other SO thread and didn't quite follow on why do you implement ServiceRegistryAwareService? The jira issue that I mentioned details that the connectionProvider instance in JDBCServicesImpl is null if the multitenancy is set to anything other than NONE and that looked like a bug. You seem to be doing something interesting with services. Can you explain in detail ? – Andy Dufresne Dec 29 '14 at 10:11

0 Answers0