Friday 21 March 2014

Grails with Mysql database DataSource.groovy configuration

Hello viewers

In this post i am going to give a sample of code snippet which helps to configure Grails with mysql database. First of all grails is a framework which is developed by integrating hibernate and spring framework by springsouce .In a grails-app to communicate with database we provide all our configuration in DataSource.groovy file present under grails-app/conf folder in a grails project


dataSource {
    pooled = true
    dbCreate = "update"
    url = "jdbc:mysql://localhost:3306/yourDb"
    driverClassName = "com.mysql.jdbc.Driver"
    username = "root"
    password = "xxxx"
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
    development {
        dataSource {
            pooled = true
            dbCreate = "update"
            url = "jdbc:mysql://localhost:3306/yourDb"
            driverClassName = "com.mysql.jdbc.Driver"
            username = "root"
            password = "xxxx"
        }
    }

and in grails we had plugins which will handle many operations.here to connect to mysql we need connector jar which in java we download and include it in our build path.But in grails right click on project in any of your IDE (Here i am using intellij idea) and in options choose grails-->plugins and search for mysql .Now install all mysql dependent plugins

then it get reflected in application.properties in your project as
plugins.mysql-connectorj=5.1.22.1

Here ends the configuration now you access mysql with your grails app


Thanks and regards
venkata naveen

No comments:

Post a Comment