Hibernate Books

All times are UTC - 5 hours [ DST ]

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: SessionFactoryImplSessionBuilderImpl multiTenancy
PostPosted: Mon Feb 27, 2012 1:23 pm 

Joined: Tue Aug 18, 2009 3:45 am
Posts: 2
When the SessionBuilderImpl opens the session, it expects to receive a tenantIdentier.

      public Session openSession() {
         return new SessionImpl(

But if I specify a CurrentTenantIdentifierResolver is the SessionFactoryImpl, I feel like it should determine the tenantIdentifier from that specific class instead of waiting for it to be initialize... ? no?

 Post subject: Re: SessionFactoryImplSessionBuilderImpl multiTenancy
PostPosted: Tue Feb 28, 2012 4:20 am 

Joined: Tue Aug 18, 2009 3:45 am
Posts: 2
Can also be around SessionFactoryImpl.openSession (around line 1008 in HB4.1.0.FINAL)

   public Session openSession() throws HibernateException {
      return withOptions().openSession();

Shouldn't it request a tenantIdentifier from the specified MultiTenantIdentifierResolver as I'm specifying the strategy as MultiTenancy.DATABASE.

AND examples from Hibernate itself tells it!

session = sessionFactory.withOptions().tenantIdentifier( "jboss" ).openSession();

From: https://github.com/hibernate/hibernate- ... yTest.java

 Post subject: Re: SessionFactoryImplSessionBuilderImpl multiTenancy
PostPosted: Wed Feb 29, 2012 12:24 pm 

Joined: Wed Feb 29, 2012 5:55 am
Posts: 6
I have the same issue. After lot's of debugging I implemented a not-quick-but-dirty workaround. I don't use the real SessionFactoryImpl directly, but only through a dynamic proxy which overrides the #openSession() by #withOptions().tenantIdentifier(...).openSession(). Because SessionFactoryImpl is final, I wasn't override the method by subclassing. Additionally I had to implement the springs interface InfrastructureProxy and add an implementation for getWrappedObject(), because otherwise Springs SpringSessionContext wouldn't be able to find it's current session, opened by HibernateTransactionMonitor (using the proxy as key) and retrieved by SpringSessionContext (using the unproxied SessionFactoryImpl as key).
AOP wasn't a solution for this, because the final-modifier prevented subclassing by CGLIB (I'm not sure whether load-time-weaving were possible) and using dynamic proxies confused the SpringSessionContext.

In a first test it looks as it works fine (even if I'm not absolutely sure and would be happy to replace this hack by Hibernate 4.1.1). Does anybody know whether it'd be enough to override #openSession() with #withOptions().tenantIdentifier(...).openSession()?

public class CurrentTenantFixLocalSessionFactoryBean extends LocalSessionFactoryBean {

   public class CurrentTenantIdentifierResolverFixWrapper implements
         InvocationHandler {

      private final String openSessionMethod="openSession";
      private final String getWrappedObjectMethod="getWrappedObject";
      private SessionFactoryImplementor factory;

      public CurrentTenantIdentifierResolverFixWrapper(
            SessionFactoryImplementor f) {
         this.factory = f;

      public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
         CurrentTenantIdentifierResolver currentTenantIdentifierResolver = factory
         if (method.getName().equals(openSessionMethod) && method.getParameterTypes().length==0 && currentTenantIdentifierResolver != null) {
            SessionBuilder sb = factory.withOptions();
            String tenant = currentTenantIdentifierResolver.resolveCurrentTenantIdentifier();
            return sb.openSession();
         } else if (method.getName().equals(getWrappedObjectMethod) && method.getParameterTypes().length==0) {
            return factory;
         } else {
            return method.invoke(factory, args);

   public SessionFactory getObject() {
      SessionFactoryImplementor realFactory = (SessionFactoryImplementor) super
      SessionFactoryImplementor proxiedFactory= (SessionFactoryImplementor) Proxy
                  new Class[] { SessionFactoryImplementor.class,
                        InfrastructureProxy.class },
                  new CurrentTenantIdentifierResolverFixWrapper(
      return proxiedFactory;

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.