Created
March 3, 2010 16:21
-
-
Save timyates/320715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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