Spring Framework
  1. Spring Framework
  2. SPR-9222

HibernateTransactionManager does not support multi tenancy, Hibernate 4.1

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: 3.1.1
    • Fix Version/s: None
    • Component/s: None
    • Labels:
    • Last commented by a User:
      false

      Description

      HibernateTransactionManager always opens new session by invoking sessionFactory.openSession(). However with multi tenancy session should be opened by sessionFactory.withOptions().tenantIdentifier(tenantIdentifier).openSession().

      I managed to get it working with a few local modifications
      1)adding new property to HibernateTransactionManager, implementation of org.hibernate.context.spi.CurrentTenantIdentifierResolver
      2)using new property if present to open session in multi tenant way
      3)modification of key used to bind session to thread
      TransactionSynchronizationManager.bindResource(new MultiKey(sessionFactory,tenantIdentifierFromResolverOrNull), txObject.getSessionHolder());
      4)modification of key used by SpringSessionContext to retrive earlier created session when callin sessionFactory.getCurrentSession() in my app code

      Unfortunately this is not very neat solution to the problem and probably has some negative, unforeseen by me consequences.

        Activity

        Hide
        Dmitri Mikhnevitch added a comment -

        Chris, I concur with Krzysztof. This issue still needs to be fixed. Out of the 4 points that were outlined in the original post it was concluded that only points 3 and 4 are not necessary.

        However, one still needs to implement workarounds 1 and 2 to make HibernateTransactionManager work with multi-tenancy-enabled SessionFactory. Otherwise out of the box it just doesn't work because tran manager opens a session without providing tenant.

        Thank you

        Show
        Dmitri Mikhnevitch added a comment - Chris, I concur with Krzysztof. This issue still needs to be fixed. Out of the 4 points that were outlined in the original post it was concluded that only points 3 and 4 are not necessary. However, one still needs to implement workarounds 1 and 2 to make HibernateTransactionManager work with multi-tenancy-enabled SessionFactory. Otherwise out of the box it just doesn't work because tran manager opens a session without providing tenant. Thank you
        Hide
        Dmitri Mikhnevitch added a comment - - edited

        Reopening the issue as per my previous comment.

        Show
        Dmitri Mikhnevitch added a comment - - edited Reopening the issue as per my previous comment.
        Hide
        Christophe Leclef added a comment -

        I agree with Krzysztof: with that suggested change, I can make Hibernate my default ORM for my SaaS platform. Without it, I still need other solution (won't use hibernate). I don't get how HibernateTransactionManager can support multi-tenancy without it.

        Show
        Christophe Leclef added a comment - I agree with Krzysztof: with that suggested change, I can make Hibernate my default ORM for my SaaS platform. Without it, I still need other solution (won't use hibernate). I don't get how HibernateTransactionManager can support multi-tenancy without it.
        Hide
        Christophe Leclef added a comment -

        Hibernate just release 4.1.4 including a fix in session opening, considering the CurrentTenantIdentifier. I'm waiting for the availability on Maven repos, and we will see https://hibernate.onjira.com/browse/HHH-7306.

        Show
        Christophe Leclef added a comment - Hibernate just release 4.1.4 including a fix in session opening, considering the CurrentTenantIdentifier. I'm waiting for the availability on Maven repos, and we will see https://hibernate.onjira.com/browse/HHH-7306 .
        Hide
        Juergen Hoeller added a comment -

        I'm considering this fixed from Spring's perspective now, since the changes in Hibernate 4.1.4 should indeed make our existing HibernateTransactionManager work with tenants - implicitly through the regular Hibernate openSession call.

        To be reopened if any issues remain.

        Juergen

        Show
        Juergen Hoeller added a comment - I'm considering this fixed from Spring's perspective now, since the changes in Hibernate 4.1.4 should indeed make our existing HibernateTransactionManager work with tenants - implicitly through the regular Hibernate openSession call. To be reopened if any issues remain. Juergen

          People

          • Assignee:
            Juergen Hoeller
            Reporter:
            Krzysztof Dworakowski
            Last updater:
            Chris Beams
          • Votes:
            5 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              1 year, 41 weeks, 1 day ago