提问者:小点点

使用多租户Hibernate的Spring Data JPA


我正在尝试让Spring Data JPA与Hibernate一起使用自定义的MultiTenantConnection Provider。

在我下面的配置中,一切似乎都正常。每次我尝试调用存储库方法时,我的< code > MultiTenantConnectionProviderImpl 类都会被调用。

主要问题是无法提供租户标识符。Spring-Data提供的存储库接口负责获取HiberNate会话。

有什么方法可以为Spring-Data提供租户标识符吗?或者有什么地方可以拦截Hibernate会话的创建,以便我们可以适当地调用< code > Session factory . with options()。tenant identifier(itend identifier)。openSession();

这是我的 Spring 配置 XML 文件。我试图尽可能保持它的基本内容。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.company"/>
    <jpa:repositories base-package="com.company.repositories"/>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="com.company.entities"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true"/>
            </bean>
        </property>
    </bean>


    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.multi_tenant_connection_provider">com.company.hibernate.MultiTenantConnectionProviderImpl</prop>
                <prop key="hibernate.multiTenancy">DATABASE</prop>
            </props>
        </property>
    </bean>


    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!--Vendor specific properties here-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
        <property name="url" value="jdbc:jtds:sqlserver://localhost:1433/myDatabase"/>
        <property name="username" value="username"/>
        <property name="password" value="password"/>
    </bean>

</beans>

共1个答案

匿名用户

使用货币特南特识别器解决方案

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="hibernateProperties">
        <map>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.SQLServer2012Dialect">
            <entry key="hibernate.format_sql" value="true">
            <entry key="hibernate.multi_tenant_connection_provider" value="com.company.hibernate.MultiTenantConnectionProviderImpl">
            <entry key="hibernate.multiTenancy" value="DATABASE">
            <!-- tenant resolver as spring bean -->
            <entry key="hibernate.tenant_identifier_resolver" value-ref="currentTenantIdentifierResolver"/>
        </map>
    </property>
</bean>

<bean id="currentTenantIdentifierResolver"
    class="com.xxx.CurrentTenantResolver">
</bean>

简单租户标识符解析器如下所示:

public class CurrentTenantResolver implements CurrentTenantIdentifierResolver {

    public String resolveCurrentTenantIdentifier() {
        // retrieve tenant from logged in user
        User usr = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal() ;       
        return usr.getTenantName();

    }

    public boolean validateExistingCurrentSessions() {
        return true;
    }

}

记住上面的类是一个spring bean,所以你可以像普通的spring bean一样自动绑定任何spring bean(service/dao)。

每次spring需要会话时,hibernate都会从bean中检索租户标识符。

相关问题