Tuesday, June 1, 2010

datasources plugin for grails

In my current project I wanted to have content and security information separated from application specific settings. I therefore wanted to store the information into separate databases. This was far easier than expected with the datasources plugin from grails. I only had to install the plugin and add the file datasources.groovy to /grails-app/conf/

datasources = { 
 datasource(name: 'ds2'){
  domainClasses([org.openlab.settings.GlobalSetting, org.openlab.settings.UserSetting, org.openlab.security.Requestmap])
  pooled(true)
  driverClassName('org.hsqldb.jdbcDriver')
  url('jdbc:hsqldb:mem:devDB')
  username('sa')
  password('')
  dbCreate('create-drop')
  hibernate {
   cache {
    use_second_level_cache(false)
    use_query_cache(false)
   }
  }  
 } 
}

The usual datasource configuration remains intact and is the core or default database. In datasources.groovy you can define as many additional databases as you like. You only have to follow this pattern where the hibernate information is included as well as the definition of domain classes you wish to store in the new database.

The clear distribution of domain classes to datasources makes this approach easy and the only thing you have to consider are foreign key relations when separating the wrong domain classes.

No comments:

Post a Comment