Friday, May 22, 2015

Grails use an external properties file for datasource configuration

Grails use an external properties file for datasource configuration


1.configure the DataSource.groovy file like this:



import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.support.PropertiesLoaderUtils

def properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource('datasource.properties'))

dataSource
pooled = true
driverClassName = properties.getProperty("dataSource.driverClassName")
username = properties.getProperty("dataSource.username")
password = properties.getProperty("dataSource.password")
url = properties.getProperty("dataSource.url")

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
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''


test
dataSource
dbCreate = "update"


production
dataSource
dbCreate = "update"



2.Create the external database configuration properties file, below is the contents of datasource.properties


dataSource.driverClassName = com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/a5go?useUnicode=true&characterEncoding=utf-8
dataSource.username=root
dataSource.password=password


Grails use an external properties file for datasource configuration

1 comment:

  1. Wow, interesting and informative post sharing about the grails use with external properties. Thank you so much. Kovai IT software solutions | Best IT company in coimbatore

    ReplyDelete