Skip to content

Instantly share code, notes, and snippets.

@dtanner
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtanner/2e2bb9e597528bbeaed8 to your computer and use it in GitHub Desktop.
Save dtanner/2e2bb9e597528bbeaed8 to your computer and use it in GitHub Desktop.
Grails Tomcat Datasource Utility
package com.foo.util
import groovy.util.logging.Log4j
import org.apache.tomcat.jdbc.pool.ConnectionPool
import org.codehaus.groovy.grails.commons.GrailsApplication
@Log4j
class TomcatDatasourceUtil {
static void ensureCurrentDatasources(GrailsApplication application, List datasourceNames) {
log.debug "Ensuring datasources are current"
datasourceNames.each { String datasourceName ->
ConnectionPool connectionPool = application.mainContext.getBean(datasourceName).targetDataSource.targetDataSource.pool
def dataSourceFileConfig = application.config."$datasourceName"
// discover the properties we want to potentially change. if changed, update and purge the pool
List propertyNames = ['url', 'username', 'password']
if (propertyNames.any { String propertyName ->
connectionPool.poolProperties."${propertyName}" != dataSourceFileConfig."${propertyName}"
}) {
connectionPool.poolProperties.url = dataSourceFileConfig.url
connectionPool.poolProperties.username = dataSourceFileConfig.username
connectionPool.poolProperties.password = dataSourceFileConfig.password
connectionPool.purge()
log.info("${datasourceName} was modified and refreshed")
} else {
log.info("${datasourceName} didn't change")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment