问题
Can anyone show me a example for multiTenant Database with Hibernate and using spring Hibernate Txmanager for tx Mgmt.
My requirements are
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
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" />
回答1:
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
来源:https://stackoverflow.com/questions/26233778/multitenant-database-in-hibernate