Grails: changing dataSource url at runtime to achieve multi tenant database separation

一笑奈何 提交于 2019-12-02 14:48:17

问题


I'm building a multi tenant application with Grails and I want to keep separate databases. I need to change the url dynamically at runtime to point GORM to different database.

I have a front-end acting as a balancer distributing requests to a cluster of backend hosts. Each backend host runs a Grails 2.3.5 instance and a mysql-server with several databases (one per tenant). I would like to change dataSource dynamically so that GORM can access domain entities on the right database.

Any ideas ?

Thanks


回答1:


You can configure multiple data source in your DataSource.groovy, have a look in the blog.

In your domains: add which data source your domain can interact, eg.,

static mapping = {
    datasources(['dataSource1', 'dataSource2'])
}

or "ALL" for all datasources, eg.,

static mapping = {
    datasource 'ALL'
}

and then you can make queries with data source name to which you want to get/set data, eg.,

def userClass = User.class
User user = userClass.dataSource1.findByName('username')

Ref:- multipleDatasources, Querying on multiple datasource in grails



来源:https://stackoverflow.com/questions/22321878/grails-changing-datasource-url-at-runtime-to-achieve-multi-tenant-database-sepa

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