Grails: can i switch case database url

How can i do sth like this in Grails 2.0.4?

dataSource {

        dbCreate = "create-drop"
        String host = request.getServerName()
        switch( host ) {
            case "company1.com": url = "jdbc:mysql://localhost/db1"
            case "company2.com": url = "jdbc:mysql://localhost/db2"
            default: null
        }
}

i don't want to use multi-tenant-core plugin

Upvotes: 1

Views: 288

Answers (1)

krock
krock

Reputation: 29619

Grails 2 supports multiple datasources so you should configure two separate data sources and let your service/domain layer do the switch between the two:

dataSource_company1 {
    dbCreate = "create-drop"
    url = "jdbc:mysql://localhost/db1"
}
dataSource_company2 {
    dbCreate = "create-drop"
    url = "jdbc:mysql://localhost/db2"
}

Then configure your domain objects to support both:

class Foo {
    static mapping = {
        datasources(['company1', 'company2'])
    }
    // ...
}

Then finally, use datasource based on request:

def company = request.serverName.split('.')[0]
def fooThings = Foo."$company".findAll()

You can also autowire your data sources into your services or controllers and use them directly.

Upvotes: 4

Related Questions