Spring或Hibernate多租户多数据库Grails应用程序 [英] Spring or Hibernate multi-tenancy for multi-database Grails app

查看:108
本文介绍了Spring或Hibernate多租户多数据库Grails应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails的多租户插件用于单个数据库,多租户插件用于多数据库,但多数据库的插件不再支持/维护。有什么办法可以将Spring或Hibernate本身用于多租户多数据库Grails应用程序吗? 您可以使用此处描述的Hibernate多租户 http:// docs。 jboss.org/hibernate/orm/4.3/devguide/en-US/html/ch16.html





你也可以考虑Spring的 AbstractRoutingDataSource


一般的想法是路由DataSource充当中介
- 而真实的DataSource可以在运行时根据查找键动态确定。

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



你可以找到一个更新的< hibernate =http://howtodoinjava.com/2013/12/28/spring-3-2-5-abstractroutingdatasource-example/ =noreferrer> post ,给出了hibernate的典型用法,您可以在以下两个代码片段中找到解决方案的要点

  public class MyRoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey(){
String language = LocaleContextHolder.getLocale()。getLanguage();
System.out.println(语言获得:+语言);
返回语言;
}
}

返回值将用作鉴别器数据源,下面的配置设置映射:

 < bean id =dataSourceclass =com.howtodoinjava.controller.MyRoutingDataSource > 
< property name =targetDataSources>
< map key-type =java.lang.String>
< entry key =envalue-ref =concreteDataSourceOne/>
< entry key =esvalue-ref =concreteDataSourceTwo/>
< / map>
< / property>
< / bean>


Grails has a multi-tenancy plugin for a single database and a multi-tenancy plugin for multi-databases, but the one for multi-databases is no longer supported/maintained. Is there some way I can instead use Spring or Hibernate themselves for a multi-tenancy multi-database Grails application?

解决方案

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>

这篇关于Spring或Hibernate多租户多数据库Grails应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆