Hibernate 4多租户和Spring 3 Hibernate

bab*_*ira 11 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

  1. Session newSession = SessionFactoryUtils.openSession(getSessionFactory( ));
  2. (Session) ReflectionUtils.invokeMethod(openSessionMethod, sessionFactory)openSession方法中
  3. Method openSessionMethod = ClassUtils.getMethod(SessionFactory.class, "openSession")以上openSessionMethod参数定义为.

我们可以看到没有钩子,你可以在打开与租户标识符的会话时按预期提供租户标识符,例如

Session newSession = getSessionFactory().withOptions().tenantIdentifier ( "abc" ).openSession();
Run Code Online (Sandbox Code Playgroud)

要么

class instance provided by hibernate property "hibernate.tenant_identifier_resolver" is called by which session is provided with tenant identifier.
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我扩展了类HibernateTransactionManager和覆盖方法doBegin,并打开了新会话,我打开了它

getSessionFactory().withOptions().tenantIdentifier ( "abc" ).openSession();    
Run Code Online (Sandbox Code Playgroud)

这使点击和工作的东西.

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

提前致谢.

Hibernate - 4.1.0 Spring - 3.1.0

Roa*_*ner 5

看起来这个功能Hibernate与您使用的版本有关:SPR-9222,HHH-7306.

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