Hibernate 4多租户和Spring 3 Hibernate

时间:2012-02-14 12:34:30

标签: java spring hibernate

所有

我正在评估Hibernate 4(4.1.0)和Spring 3(3.1.0)中出现的Multi-Tenancy功能,但是无法使用HibernateTransaction设置。我已经定义了如下设置。

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>

如果我使用Spring 3提供的普通HibernateTransactionManager,我会收到错误租户标识符,原因是这样,它打开一个会话如下

  1. Session newSession = SessionFactoryUtils.openSession(getSessionFactory( ));
  2. (Session) ReflectionUtils.invokeMethod(openSessionMethod, sessionFactory)方法中
  3. openSession
  4. Method openSessionMethod = ClassUtils.getMethod(SessionFactory.class, "openSession")以上openSessionMethod参数定义为。
  5. 我们可以看到没有钩子可以在打开具有租户标识符的会话时按预期提供租户标识符,例如

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

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

    为了克服这个问题,我扩展了类HibernateTransactionManager并重写了方法doBegin,并且打开了新会话,我打开了它

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

    这使得点击并工作。

    我只是想知道,上述方法是否合适,或者有一些我不知道的设置使其开箱即用。

    提前致谢。

    Hibernate - 4.1.0 春天 - 3.1.0

1 个答案:

答案 0 :(得分:5)

使用您使用的版本Hibernate方看起来此功能有问题:SPR-9222HHH-7306

Hibernate版本4.1.4开始您应该使用CurrentTenantIdentifierResolver将当前租户ID传递给SessionFactory