-->
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.  [ 6 posts ] 
Author Message
 Post subject: "current session" for multitenant application?
PostPosted: Mon Jul 28, 2014 9:40 am 
Newbie

Joined: Mon Jul 28, 2014 9:18 am
Posts: 6
We developped several multitenant Java web applications. The multitenancy model was "database per tenant", meaning that each tenant has its own database. Actually for the webapp each tenant is seen as a distinct JDBC datasource, so the model is more like "datasource per tenant".

Each time we faced the same requirement: being able to read or write data from multiple tenants within the same transaction (for example to display in a single web page stats about all the tenants, or to change some access rights for multiple tenants at once). This means that during a single web request, we have to be able to use multiple sessions of the same session factory.

I think the Hibernate 4.1.12 implementations of "current session" do not support this "sessions switching":
    ThreadLocalSessionContext: contains a ThreadLocal<Map> but the map is Map<SessionFactory, Session> so for a given session factory we can have at most one session.
    ManagedSessionContext: also contains a ThreadLocal<Map<SessionFactory,Session>>.
    JTASessionContext: contains a Map but the map is Map<Object, Session> where the Object is the transactionId, so once again one session at most.

So we had to implement our own CurrentSessionContext.

My question is: does Hibernate support, out of the box, using multiple sessions from the same session factory during a single web request? If yes, how?

Thank you for your time.


Top
 Profile  
 
 Post subject: Re: "current session" for multitenant application?
PostPosted: Wed Apr 29, 2015 7:38 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Is you code relying on
Code:
SessionFactory.getCurrentSession()
?
Because if you explicitly set an tenant id, the classes you mention should not have to be invoked I think.

Code:
Session session1 = sessionFactory.withOptions()
        .tenantIdentifier( tenant1 )
        ...
        .openSession();
Session session2 = sessionFactory.withOptions()
        .tenantIdentifier( tenant2 )
        ...
        .openSession();

//have fun with session1 and session2


Is that what is not working?

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: "current session" for multitenant application?
PostPosted: Thu Apr 30, 2015 5:06 am 
Newbie

Joined: Mon Jul 28, 2014 9:18 am
Posts: 6
I'm not sure I correctly understood your point. :)

I'd like to keep the "currentSession()" mechanism to reuse sessions from the same web request, because explicit calls to "openSession()" will create new sessions.

I'd need something such as:
Code:
// Beginning of web request

[In myBusinessService] sessionFactory.setCurrentTenantIdentifier("tenant1"); // or something that sets the current tenant ID
[In myDao] sessionFactory.currentSession().doSomething();

[In myBusinessService] sessionFactory.setCurrentTenantIdentifier("tenant2"); // or something that sets the current tenant ID
[In myDao] sessionFactory.currentSession().doSomething();

// Possibly work again with tenant1 and/or tenant2

// End of web request


Is this pattern supported ?


Top
 Profile  
 
 Post subject: Re: "current session" for multitenant application?
PostPosted: Thu Apr 30, 2015 9:16 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Ok got it now, no it's not supported.
Code:
sessionFactory.setCurrentTenantIdentifier("tenant1");
would be a bad approach as it does not necessary make this thread safe but one could imagine a
Code:
CurrentTenantIdentifierResolver
implementation that returns the right tenant id based on some user input.

I have opened https://hibernate.atlassian.net/browse/HHH-9766 to discuss this.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: "current session" for multitenant application?
PostPosted: Thu May 07, 2015 7:09 am 
Newbie

Joined: Mon Jul 28, 2014 9:18 am
Posts: 6
Ok, thank you for your answer and for the feature request. :)


Top
 Profile  
 
 Post subject: Re: "current session" for multitenant application?
PostPosted: Fri May 08, 2015 1:52 pm 
Newbie

Joined: Mon Jul 28, 2014 9:18 am
Posts: 6
I think using multiple sessions (being tied to a multiple tenants) from the SAME session factory is in fact doomed to fail, as it does not support second second level caching (and also session factory stats).

More precisely considering that the second level cache is at the session factory level, I think data cached on behalf of one tenant would be visible to other tenants, hence breaking the isolation of the tenants.

Can you confirm my explanation ?

If confirmed, you can remove the feature request for me because the caching issue is a too important limitation.

As a consequence each tenant must have its own session factory, and the present Hibernate implementations of "current session" are enough to support the uses cases I mentioned in my first post.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.