Spring或Hibernate多租户多数据库Grails应用程序 [英] Spring or Hibernate multi-tenancy for multi-database Grails app
问题描述
或
你也可以考虑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屋!