Datasource and hibernate configuration in grails app

Posted By : Shakil Pathan | 21-Dec-2014

Hi Everyone,

In this blog, I am going to explain you about the datasource file(DataSource.groovy) and its configuration to effeciently use databases and hibernate framework in your grails app.

And also explain you about the options and configuration used for your grails app. Below I have used some options and explained it in the comment lines.

dataSource {
    driverClassName = "com.mysql.jdbc.Driver"	//The class name of the JDBC driver
    username = "root"	//The username used to establish a JDBC connection
    password = "root"	//The password used to establish a JDBC connection
}
hibernate {
    cache.use_second_level_cache = true
	//turn on the second-level cache (by default it is configured to off - 'false' - by Hibernate)
    cache.use_query_cache = false
	//true for turn on the Hibernate query cache
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
//    cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
}
dataSource {
            dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
            url = "jdbc:mysql://localhost:3306/trendrsite"	//The JDBC URL of the database
            properties {
			   maxActive = -1
			   //The maximum number of active connections that can be allocated from this pool at the same time, or non-positive for no limit
			   minEvictableIdleTimeMillis=1800000
			   //The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any)
			   timeBetweenEvictionRunsMillis=1800000
			   //The number of milliseconds to sleep between runs of the idle object evictor thread
			   numTestsPerEvictionRun=3
			   //The number of objects to examine during each run of the idle object evictor thread (if any)
			   testOnBorrow=true
			   //The indication of whether objects will be validated before being borrowed from the pool
			   testWhileIdle=true
			   //The indication of whether objects will be validated by the idle object evictor (if any)
			   testOnReturn=false
			   //The indication of whether objects will be validated before being returned to the pool
			   validationQuery="SELECT 1"
			   //The SQL query that will be used to validate connections from this pool before returning them to the caller
			   jdbcInterceptors="ConnectionState"
			   //Interceptor that keep track of connection state to avoid roundtrips to the database and helps cache the state
			}
        }

Hope It helps!

Thanks

Shakil

About Author

Author Image
Shakil Pathan

Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .

Request for Proposal

Name is required

Comment is required

Sending message..