我正在評估多租戶目前在休眠4(4.1.0)功能使用Spring 3(3.1.0)一起,但無法得到它使用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>
如果使用由彈簧3提供我得到錯誤租戶標識符不設置純HibernateTransactionManager的,原因之中,它打開會話如下
Session newSession = SessionFactoryUtils.openSession(getSessionFactory());
(Session) ReflectionUtils.invokeMethod(openSessionMethod, sessionFactory)
在openSession
方法
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();
這使得東西點擊和工作。
我只是想知道,這是上面的方法好還是有一些設置,我不知道這使得它開箱即用。
在此先感謝。
休眠 - 4.1.0 春天 - 3.1.0
是否有可能顯示如何在示例maven應用程序中的一切工作?如果您與我們分享,我將非常感激。 – Maksim 2012-09-27 22:47:46