休眠中的multiTenant数据库 [英] multiTenant database in hibernate
问题描述
任何人都可以向我展示一个使用Hibernate的multiTenant数据库示例,以及如何使用Spring Hibernate Txmanager进行tx管理.
Can anyone show me a example for multiTenant Database with Hibernate and using spring Hibernate Txmanager for tx Mgmt.
我的要求是
其他主数据库始终处于打开状态,并包含有关租户Db的信息. 第一次访问master时,获取特定租户的db信息,并为租户生成会话ZFactory,其余请求应通过租户会话提供. 我已经实现了
thers master database which is always open and contains info about tenant Db. on hitting master for first time get db info of specific tenant and generate session ZFactory for the tenant and rest request shall be served via tenant Sessions. i have implementated
但是没有让我的交易经理为租户Dbs工作.
but cudn't get my transaction manager working for tenant 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" />
推荐答案
这是我在Spring论坛上的解决方案(我的昵称是deggesim)->
This is my solution at Spring Forum (my nickname is deggesim) -> http://forum.spring.io/forum/spring-projects/data/114116-sessionfactory-configured-for-multi-tenancy-but-no-tenant-identifier-specified
这篇关于休眠中的multiTenant数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!