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.