Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

Externalizing Grails Datasource configuration

Grails 1.x allows using external configuration files by setting the grails.config.locations directive. Is there a similar approach available for externalizing the database configuration in Datasource.groovy (without setting up JNDI)?

It would prove helpful to be able to configure DB credentials in a simple configuration file outside the application.

Thanks in advance!

question from:https://stackoverflow.com/questions/970133/externalizing-grails-datasource-configuration

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use a properties file specified in the grails.config.locations as a way to externalize the datasource configuration. Below is how I typically set up a Grails project:

In my DataSource.groovy I specify this for the production environment:

  ....
  ....
  production {
    dataSource {
        dbCreate = "update"
        driverClassName = "com.myorg.jdbcDriverNotExists"
        url = ""
        username = ""
        password = ""
    }
  }
  ....
  ....

I specify an external properties file in my Config.groovy:

grails.config.locations = [ "classpath:app-config.properties"]

In the properties file (stored in grails-app/conf/) I specify the actual datasource info:

dataSource.driverClassName=oracle.jdbc.OracleDriver
dataSource.url=jdbc:oracle:thin:@host:port:sid
dataSource.username=sa
dataSource.password=secret

I also use the properties file as a way to override other values that are in Config.groovy. When the app is deployed, if I have to modify the datasource info I just edit the /WEB-INF/classes/app-config.properties file and restart.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...