Sunday, June 19, 2011

GRAILS Configuration -- DataSource

The dataSource.groovy file is used for database configuration within Grails. Grails by default comes bundled with HSQLDB. The are three basic environments and settings available for configuration.

ENVIRONMENTS
  • development
  • test
  • production

SETTINGS / DbCreate
  • create-drop : Creates all the tables when application is started and drops them when application shuts down.
  • create : Creates all the tables when application starts.
  • update : Updates already created tables. Used in production mode.
URL : This option specifies which url and which database to hit for a particular environment.

As soon as a new app is created, the datasource file will be configured to HSQLDB. Below is the code.


dataSource {
    pooled = true
    driverClassName = "org.hsqldb.jdbcDriver"
    username = "sa"
    password = ""
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = true
    cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
}
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop','update'
            url = "jdbc:hsqldb:mem:devDB"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:hsqldb:mem:testDb"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:hsqldb:file:prodDb;shutdown=true"
        }
    }
}


  1. First section specifies the driver of the database and also login credentails.
  2. Second section is for hibernate settings.
  3. Third section is for dbCreate and url settings for each of the environment.
For configuring the datasource to MYSQL, Firstly the MYSQL JDBC Connector JAR needs to be added to the lib folder. The JAR can be found here. So, finally the datasource for MYSQL could be something like below.


dataSource {
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    username = "root"
    password = ""
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = true
    cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
}
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop','update'
            url = "jdbc:mysql://localhost/devDb"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:mysql://localhost/testDb"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:mysql://localhost/prodDb"
        }
    }
}


Also, when we do a Grails run-app it runs the application in dev environment by default.
So, in order to run it in test environment use


grails run-app (will run in development environment by default)
grails test run-app (will run in test environment)
grails [env]* run-app (Basic Syntax)



No comments:

Post a Comment