![](/img/trans.png)
[英]Multi-Tenancy with Spring + Hibernate: "SessionFactory configured for multi-tenancy, but no tenant identifier specified"
[英]Hibernate 4 Multi-Tenancy and Spring 3 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,我会收到错误租户标识符,原因是这样,它打开一个会话如下
Session newSession = SessionFactoryUtils.openSession(getSessionFactory( ));
(Session) ReflectionUtils.invokeMethod(openSessionMethod, sessionFactory)
在openSession
方法中的(Session) ReflectionUtils.invokeMethod(openSessionMethod, sessionFactory)
Method openSessionMethod = ClassUtils.getMethod(SessionFactory.class, "openSession")
openSessionMethod
参数定义为。 我们可以看到没有钩子,你可以在打开与租户标识符的会话时按预期提供租户标识符,例如
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 Spring - 3.1.0
看起来这个功能在Hibernate
方面与您使用的版本有关: SPR-9222 , HHH-7306 。
从Hibernate
版本4.1.4
您应该使用CurrentTenantIdentifierResolver
将当前租户ID传递给SessionFactory
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.