1
任何人都可以給我看一個multiTenant數據庫與Hibernate的例子,並使用Spring Hibernate Txmanager進行tx Mgmt。多帳戶數據庫在休眠
我的要求是
thers主數據庫,始終是敞開的,幷包含租戶Db的信息。 第一次打主人獲取特定承租人的數據庫信息,併爲承租人生成會話ZFactory,其餘的請求應通過承租人會話提供。 我實施了
但我不能讓我的交易管理員爲租戶Dbs工作。
<property name="packagesToScan">
<array>
<value>beans.table</value>
<value>beans.views</value>
</array>
</property>
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<!-- <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop> -->
<prop key="c3p0.testConnectionOnCheckout">false</prop>
<prop key="c3p0.min_size">2</prop>
<prop key="c3p0.max_size">10</prop>
<prop key="c3p0.timeout">300</prop>
<prop key="c3p0.max_statements">50</prop>
<prop key="c3p0.idleTestPeriod">300</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>
<bean id="tenantSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" lazy-init="true">
<property name="packagesToScan">
<array>
<value>beans.table</value>
<value>views</value>
</array>
</property>
<!--<property name="dataSource" ref="dataSource" />-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<!-- <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop> -->
<prop key="c3p0.testConnectionOnCheckout">false</prop>
<prop key="c3p0.min_size">2</prop>
<prop key="c3p0.max_size">30</prop>
<prop key="c3p0.timeout">300</prop>
<prop key="c3p0.max_statements">50</prop>
<prop key="c3p0.idleTestPeriod">300</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.multiTenancy">DATABASE</prop>
<prop key="hibernate.tenant_identifier_resolver">factory.MultiTenantIdentifierResolver</prop>
<prop key="hibernate.multi_tenant_connection_provider">factory.MultiTenantConnectionProvider</prop>
</props>
</property>
</bean>
<!-- Declare a transaction manager -->
<!-- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" /> spring.fix.HibernateTransactionManager
com.my.hibernate4.spring.fix.HibernateTransactionManager"> <property name="sessionFactory"
ref="sessionFactory" /> -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="tenantTransactionManager"
class="factory.MultiTenantHibernateTxManager" p:autodetectDataSource="false">
<property name="sessionFactory" ref="tenantSessionFactory" />
</bean>
<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true"/>
<tx:annotation-driven transaction-manager="tenantTransactionManager"
proxy-target-class="true" />
不應連接到根數據庫。相反,您應該首先直接連接到正確的pdb。 – steve 2014-10-07 18:34:11
連接到根數據庫我已經在MultiTenantConnectionProvider實現類中創建了不同的會話工廠,所以我從那裏獲取DataSourceDynmically併爲tenantSessionFactory提供Connection。 – 2014-10-08 04:55:49
我無法直接連接到數據庫..因爲我的應用程序登錄是基於根數據庫和其餘部分是基於tenantDb。基於支持特定租戶的用戶。 – 2014-10-08 04:56:54