Spring or Hibernate multi-tenancy for multi-database Grails app

 ̄綄美尐妖づ 提交于 2019-12-05 13:36:32

You can use Hibernate multitenancy described here http://docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html/ch16.html

OR

You can also consider Spring's AbstractRoutingDataSource

The general idea is that a routing DataSource acts as an intermediary - while the ‘real’ DataSource can be determined dynamically at runtime based upon a lookup key.

https://spring.io/blog/2007/01/23/dynamic-datasource-routing/

You can find a newer post, giving an exemplary use with hibernate, the gist of the solution you can find in the following two snippets

public class MyRoutingDataSource extends AbstractRoutingDataSource{
    @Override
    protected Object determineCurrentLookupKey() {
        String language = LocaleContextHolder.getLocale().getLanguage();
        System.out.println("Language obtained: "+ language);
        return language;
    }
}

the return value will be used as a discriminator for a datasource, the following configuration sets the mapping

<bean id="dataSource" class="com.howtodoinjava.controller.MyRoutingDataSource">
   <property name="targetDataSources">
      <map key-type="java.lang.String">
         <entry key="en" value-ref="concreteDataSourceOne"/>
         <entry key="es" value-ref="concreteDataSourceTwo"/>
      </map>
   </property>
</bean>

In our case we use LocalContainerEntityManagerFactoryBean where we create a multiTenantMySQLProvider.

 <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="packagesToScan" value="domain"/>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
            <entry key="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />
            <entry key="hibernate.show_sql" value="false" />
            <entry key="hibernate.multiTenancy" value="SCHEMA" />
            <entry key="hibernate.multi_tenant_connection_provider" value-ref="mySQLMultiTenantConnectionProvider" />
            <entry key="hibernate.tenant_identifier_resolver" value-ref="tenantIdentifierResolver" />
        </map>
    </property>
</bean>




   <bean id="tenantService"
      class="multitenancy.service.impl.TenantServiceImpl">
    <property name="defaultTenantId" value="${multitenancy.defaultTenantId}" />
    <property name="ldapTemplate" ref="ldapTemplate" />
</bean>

<bean id="connectionProvider"
      class="multitenancy.hibernate.ConnectionProviderImpl"  lazy-init="false">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="mySQLMultiTenantConnectionProvider"
      class="multitenancy.hibernate.MySQLMultiTenantConnectionProviderImpl" lazy-init="false">
    <property name="connectionProvider" ref="connectionProvider" />
    <property name="tenantIdentifierForAny" value="${multitenancy.tenantIdentifierForAny}" />
    <property name="schemaPrefix" value="${multitenancy.schemaPrefix}" />
</bean>

<bean id="tenantIdentifierResolver"
      class="multitenancy.hibernate.TenantIdentifierResolverImpl" lazy-init="false">
    <property name="tenantService" ref="tenantService" />
</bean>

<bean id="tenantIdentifierSchedulerResolver"
      class="security.impl.TenantIdentifierSchedulerResolverImpl" lazy-init="false">
    <property name="ldapTemplate" ref="ldapTemplate" />
</bean>

And here the implementation of the MySQLMultiTenantConnectionProviderImpl

public class MySQLMultiTenantConnectionProviderImpl implements MultiTenantConnectionProvider, ServiceRegistryAwareService, Stoppable {


private static final Logger LOGGER = LoggerFactory.getLogger(MySQLMultiTenantConnectionProviderImpl.class);

@Setter
private ConnectionProvider connectionProvider;

@Setter
private String tenantIdentifierForAny;

@Setter
private String schemaPrefix;

@Override
public Connection getAnyConnection() throws SQLException {
    return connectionProvider.getConnection();
}

@Override
public void releaseAnyConnection(Connection connection) throws SQLException {
    connectionProvider.closeConnection( connection );
}

@Override
public Connection getConnection(String tenantIdentifier) throws SQLException {
    final Connection connection = getAnyConnection();
    String schema = schemaPrefix + tenantIdentifier;
    try {
        LOGGER.debug("setting schema in DB Connection : {}"  , schema);
        connection.createStatement().execute( "USE " + schema );
    }
    catch ( SQLException e ) {
        throw new HibernateException(
           "Could not alter JDBC connection to specified schema [" + schema + "]", e
        );
    }
    return connection;
}

@Override
public void releaseConnection(String tenantIdentifier, Connection connection) throws SQLException {
    try {
        connection.createStatement().execute( "USE " + tenantIdentifierForAny );
    }
    catch ( SQLException e ) {
        LOGGER.error(" error on releaseConnection. The connection will be not closed. SQLException : {}" , e);
        // on error, throw an exception to make sure the connection is not returned to the pool.
        throw new HibernateException(
            "Could not alter JDBC connection to specified schema [" + tenantIdentifier + "]", e
        );
    }
    // I follow the hibernate recommendation and we don't return the connetion to the pool.
    connectionProvider.closeConnection( connection );
}

@Override
public boolean supportsAggressiveRelease() {
    return true;
}

@Override
public void stop() {

}

@Override
public boolean isUnwrappableAs(Class unwrapType) {
    return ConnectionProvider.class.equals( unwrapType ) ||
            MultiTenantConnectionProvider.class.equals( unwrapType ) ||
            AbstractMultiTenantConnectionProvider.class.isAssignableFrom( unwrapType );
}

@Override
public <T> T unwrap(Class<T> unwrapType) {
    if ( isUnwrappableAs( unwrapType ) ) {
        return (T) this;
    }
    throw new UnknownUnwrapTypeException( unwrapType );

}

@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {


}

}

This is how I use hibernate multitenancy with SCHEMA approach. May be it will be helpful for you.

applicationContext.xml

    ...
    <bean id="multiTenantConnectionProvider" class="org.myapp.MyAppMultiTenantConnectionProvider"/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan" value="org.myapp.entities"/>
        <property name="multiTenantConnectionProvider" ref="multiTenantConnectionProvider"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.multiTenancy">SCHEMA</prop>
                <prop key="hibernate.tenant_identifier_resolver">org.myapp.MyAppTenantIdentifierResolver</prop>
                ...
            </props>
        </property>
    </bean>
    ...

MyAppMultiTenantConnectionProvider.java

public class MyAppMultiTenantConnectionProvider implements MultiTenantConnectionProvider {

    @Override
    public Connection getConnection(String tenantIdentifier) throws SQLException {
        //This is where your tenant resolving logic will be implemented
        return MyMultitenantConnectionPool.getConnection(tenantIdentifier);
    }
}

MyAppTenantIdentifierResolver.java

public class MyAppTenantIdentifierResolver implements CurrentTenantIdentifierResolver {

    @Override
    public String resolveCurrentTenantIdentifier() {
        /*
        This is where you determine which tenant to use.
        In this app SpringSecurity used for this purpose.
        TenantUser class extends org.springframework.security.core.userdetails.User with tenant information.   
        */ 
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) return "";

        if (authentication.getPrincipal() instanceof TenantUser) {
            TenantUser user = (TenantUser) authentication.getPrincipal();
            return user.getTenant();
        } else return "";
    }

    @Override
    public boolean validateExistingCurrentSessions() {
        return false;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!