Follow me

Thursday, September 13, 2012

GRAILS DATABASE AND CONFIG PROPERTIES EXTERNALIZATION

In Grails' DataSource descriptor file located at grails-app/conf/DataSource.groovy.
This file contains the dataSource definition which includes the database settings like
driverClassName ,username ,password ,url  etc.....

Since Grails' DataSource definition is "environment aware", so you can do it like:

local {
                              dataSource {
                                             dbCreate = "update" // one of 'create', 'create-drop','update'
                                             url = "jdbc:mysql://localhost/dbname"                   
                                             username = "username"
                                             password = "password"
                              }
               }
production {
                              dataSource {
                                             dbCreate = "update" // one of 'create', 'create-drop','update'
                                             url = "jdbc:mysql://hostname/dbname"                  
                                             username = "username"
                                             password = "password"
                              }
               }

We can externalize these settings by creating the separate property file to set above parameter.
Below are the steps I follow to externalize the database parameter in grails application.

Step 1)
Set one System property which is path to your externalize property file or dir
                          JAVA_OPTS="$JAVA_OPTS -DextConfigDir=/path/to/ext/prop"
Step 2)
                       In Config.groovy add the below chunk of code. This will search for config files that get
                       merged into the main config.
                              def extConfig = System.properties.getProperty('extConfigDir')
                              grails.config.locations = ["file:${userHome}/.grails/${appName}-config.groovy"]
                              if (extConfig) {
                                             grails.config.locations << "file:${extConfig}/Config.properties"
                              }
                 
Step 3)
                    Creating the externalize property file.
                    Remove the datasource properties from DataSource.groovy and set this properties in
                    Config.properties mentioned below
                       Ex: - In DataSource.groovy
                              local {
                                             dataSource {
                                                            url = "jdbc:mysql://localhost/dbname"                   
                                                            username = "username"
                                                            password = "password"
                                             }
                              }
                              The Exteralize Config.properties file will be
                              dataSource.url=jdbc:mysql://localhost/dbname
                              dataSource.username=username
                              dataSource.password=password

 In this way we can externalize the data source as well as Config.groovy properties

No comments:

Post a Comment