在DataSources.groovy中扩展多个Grails DataSource?

时间:2011-03-16 21:15:09

标签: grails groovy datasource

您在DataSource.groovy中定义的每个环境都会在环境之外的文件根目录中扩展基本dataSource定义。

我有两个特定的配置需要应用于许多不同环境的环境特定更改。实际上,我需要两个“基本定义”,或者某种方式来扩展现有定义。

我该怎么办?

dataSource1 {
    dbCreate = "update"
    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"
}

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

3 个答案:

答案 0 :(得分:4)

以下内容将为dataSource1和dataSource2分配一个Closure(注意=),然后您可以在环境块中调用它们。

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()
        }
    }
}

答案 1 :(得分:0)

dataSource闭包可以存在于环境闭包中......

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

答案 2 :(得分:0)

您可以使用Grails的外部配置文件(而不是Config.groovy)并在其中定义dataSource。为此,我们可以在Config.groovy中编写以下代码

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

然后在部署的环境中,定义环境变量:${appName}.config.location。该环境变量指向外部配置文件。