Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8514269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:49:01+00:00 2026-06-11T04:49:01+00:00

All I was evaluating Multi-Tenancy feature present in Hibernate 4(4.1.0) along with Spring 3(3.1.0),

  • 0

All

I was evaluating Multi-Tenancy feature present in Hibernate 4(4.1.0) along with Spring 3(3.1.0), but was not able to get it working with HibernateTransaction setting. I have defined settings as follows.

LocalSessionFactoryBean :

@org.springframework.context.annotation.Configuration
public class Configuration {

    @Inject private DataSource dataSource;
    @Inject private MultiTenantConnectionProvider multiTenantConnectionProvider;

    @Bean
    public LocalSessionFactoryBean sessionFactory() throws IOException{
        LocalSessionFactoryBean bean = new LocalSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setPackagesToScan("com");
        bean.getHibernateProperties().put("hibernate.multi_tenant_connection_provider", multiTenantConnectionProvider);
        bean.getHibernateProperties().put("hibernate.multiTenancy", "SCHEMA");
        bean.getHibernateProperties().put("hibernate.tenant_identifier_resolver", new CurrentTenantIdentifierResolverImpl());
        bean.setConfigLocation(new ClassPathResource("/hibernate.cfg.xml"));
        return bean;
    }

}

Configuration.xml :

<context:component-scan base-package="com.green" />


    <context:annotation-config />

    <!-- Enable annotation style of managing transactions -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- Declare a datasource that has pooling capabilities -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close" p:driverClass="${app.jdbc.driverClassName}"
        p:jdbcUrl="${app.jdbc.url}" p:user="${app.jdbc.username}" p:password="${app.jdbc.password}"
        p:acquireIncrement="5" p:idleConnectionTestPeriod="60" p:maxPoolSize="100"
        p:maxStatements="50" p:minPoolSize="10" />

    <!-- Declare a transaction manager -->
    <bean id="transactionManager"
        class="com.green.saas.hibernate.SaasHibernateTransactionManager"
        depends-on="sessionFactory" >
          <property name="sessionFactory" ref="sessionFactory"></property>
          <property name="dataSource" ref="dataSource"></property>
    </bean>

If I use plain HibernateTransactionManager provided by spring 3 I get error tenant identifier not set, reason for this being, it opens a session as follows

  1. Session newSession = SessionFactoryUtils.openSession(getSessionFactory( ));
  2. (Session) ReflectionUtils.invokeMethod(openSessionMethod, sessionFactory) in openSession method
  3. Method openSessionMethod = ClassUtils.getMethod(SessionFactory.class, "openSession") above openSessionMethod argument is defined as.

we can see that there is no hook where you can provide tenant-identifier as expected while opening a session with tenant-identifier e.g

Session newSession = getSessionFactory().withOptions().tenantIdentifier ( "abc" ).openSession();

or

class instance provided by hibernate property "hibernate.tenant_identifier_resolver" is called by which session is provided with tenant identifier.

To overcome this I extended class HibernateTransactionManager and override method doBegin and where new session is opened I opened it

getSessionFactory().withOptions().tenantIdentifier ( "abc" ).openSession();    

this makes thing click and work.

I just wanted to know, is this above approach fine or there is some setting that I am not aware of that makes it work out of the box.

Thanks in advance.

Hibernate – 4.1.0
Spring – 3.1.0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:49:02+00:00Added an answer on June 11, 2026 at 4:49 am

    It looks like this feature was buggy on Hibernate side with the version you used: SPR-9222, HHH-7306.

    Starting from Hibernate version 4.1.4 You should use CurrentTenantIdentifierResolver to pass the current tenant ID to SessionFactory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

all. We're trying to get some intersect collisions working, but the problem experience is
I'm looking for a succinct expression for evaluating whether or not all the items
Possible Duplicate: Does the compiler continue evaluating an expression where all must be true
I'm evaluating whether WF would be a good fit for a design I'm working
I'm currently evaluating eclipse after using Textmate for all my development for many years.
I'm evaluating a database for my next project. I want to store all the
Possible Duplicate: Does Oracle fetch all the rows before evaluating rownum? If I run
My company is evaluating CSLA.NET as a possible standard framework for all our .NET
I'm evaluating Hazelcast as a distributed data grid solution for an application I'm working
I'm evaluating a migration to pdo on some hosts (VPS) but it seems to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.