Skip to content

Instantly share code, notes, and snippets.

@timyates
Created March 3, 2010 16:21
Show Gist options
  • Save timyates/320715 to your computer and use it in GitHub Desktop.
Save timyates/320715 to your computer and use it in GitHub Desktop.
// This in BootStrap.groovy
class BootStrap {
def dataService
def init = { servletContext ->
dataService.initBeans()
}
def destroy = {
}
}
// Then this is the DataService
class DataService implements ApplicationContextAware {
ApplicationContext applicationContext
boolean transactional = false
// Get an Sql instance for a DataSource
def getSql( species, version ) {
Sql.newInstance( applicationContext.getBean( "${species}_${version}_db" ) )
}
// Register all BasicDataSource beans
def initBeans() {
Species.list().each { species ->
species.versions.each { version ->
registerBean( species, version )
}
}
}
// Register a single bean
def registerBean( Species species, SpeciesVersion version ) {
def bb = new grails.spring.BeanBuilder()
bb.beans {
"${species.internalName}_${version.name}_db"( BasicDataSource ) {
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost/${species.dbName}_${version.name}"
username = "XXXXXXXXX"
password = "XXXXXXXXX"
}
}
bb.registerBeans( applicationContext )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment