开发者

Extending multiple grails DataSources in DataSources.groovy?

开发者 https://www.devze.com 2023-02-17 12:03 出处:网络
Every environment you define in DataSource.groovy extends the base dataSource definition at the root of the file, outside of environments.

Every environment you define in DataSource.groovy extends the base dataSource definition at the root of the file, outside of environments.

I have two specific configurations that I need to apply to a number of different environments which have minor environment-specific changes. Really, I need two "base definitions", or some way to extend existing definitions.

How do I do what I'm trying to do?

dataSource1 {
    dbCreate = "update"
    dialect = org.hibernate.dialect.Oracle10gDialect
    pooled = false
    jndiName = "java:something"
}
dataSource2 {
            pooled = true
            driverClassName = "org.hsqldb.jdbcDriver"
            usern开发者_StackOverflow社区ame = "sa"
            password = ""
            dbCreate = "update"
            url = "jdbc:hsqldb:mem:testDb"
}

// environment specific settings
environments {
    //extend datasource1
    production{
    }
    //extend datasource2
    development{
    }
}


The following will assign dataSource1 and dataSource2 a Closure (note the =) and you could then call them within your environment blocks.

dataSource1 = {
    dbCreate = "update"
    driverClassName = "org.hsqldb.jdbcDriver"
    dialect = org.hibernate.dialect.Oracle10gDialect
    pooled = false
    jndiName = "java:something"
}

dataSource2 = {
            pooled = true
            driverClassName = "org.hsqldb.jdbcDriver"
            username = "sa"
            password = ""
            dbCreate = "update"
            url = "jdbc:hsqldb:mem:testDb"
}

environments {
    production {
        dataSource {
            dataSource1.call()
        }
    }
    development {
        dataSource {
            dataSource2.call()
        }
    }
}


The dataSource closure can exist within the environemnts closures...

environments {
   production {
      dataSource {
         dbCreate = "update"
         dialect = org.hibernate.dialect.Oracle10gDialect
         pooled = false
         jndiName = "java:something"
      }
   }
}


You can use an outside config file for Grails(instead of Config.groovy) and define the dataSource inside it. To do that, we can write the following code in Config.groovy

if (System.properties["${appName}.config.location"]) {
    grails.config.locations = ["file:" + System.properties["${appName}.config.location"]]
}

Then at the deployed environment, define the environment variable: ${appName}.config.location. That environment variable point to the outside config file.

0

精彩评论

暂无评论...
验证码 换一张
取 消