Really enjoying Grails (Multiple datasources without GORM example)

Lately I’ve been trying out Grails. It’s a terrific framework for creating web applications and written in Groovy/Java. It’s similar in a lot of ways to Ruby on Rails (written in Ruby). Since I already work a lot with Java and I just prefer Groovy’s syntax to that of Ruby I decided on using Grails (this is a very personal decision, both are terrific and I urge you to try one of them if you are into creating web applications!).

Grails uses Hibernate and Spring as underlying technologies. While those were already great to use in Java,I really hated the amount of xml configuration required. Grails makes things soooo much easier.

Here is an example of how I used Grails’ Spring DSL and auto-wiring amongst other things. I wanted to create a monitoring application that had to extract information from one of several datasources based on the choice of the user. So all datasources needed to be available in the application and I should be able to select which one to use dynamically. (Btw, there are no domain objects associated with those datasources so I didn’t need GORM)

Here are some code snippets:

First up is resources.groovy (which is in the conf/spring directory). In this file you can declare extra Spring configuration that’s needed using the BeanBuilder DSL.

Look ma! No XML!

import org.apache.commons.dbcp.BasicDataSource
// Place your Spring DSL code here
beans = {
 dataSourceTest(BasicDataSource) {
 	driverClassName = "oracle.jdbc.driver.OracleDriver"
 	url = "jdbc:oracle:thin:@localhost:1521:TEST"
 	username = "someone"
 	password = "something"
 }
 dataSourceProduction(BasicDataSource) {
 	driverClassName = "oracle.jdbc.driver.OracleDriver"
 		url = "jdbc:oracle:thin:@localhost:1521:PROD"
 	username = "someone"
 	password = "something"
 }
}

So, that was easy, and no XML involved! So how do we get to those datasources in the application? Well, for this task I created a Grails service. Services in Grails are by default singletons. That’s why there’s no ‘static’ keyword to be found which is often used in java. Also, when you look at the code you’ll notice that I define the variables for the datasources but I never load them. This is another great feature of Grails. It auto-wires the datasources to the variables because they have the same name, so only declaring the variables is enough. That’s it.Here is DatabaseService.groovy:


class DatabaseService {

    def TEST = "Test"
    def PRODUCTION = "Production"

    def dataSourceTest
    def dataSourceProduction

    boolean transactional = true

    def getDataSource(dbEnv) {
        switch(dbEnv) {
            case TEST:
                return dataSourceTest
            case PRODUCTION:
                return dataSourceProduction
        }
    }
}

Great, now we have a way to get to the datasources. Now to actually use them. In the next code block another example of the Grails auto-wiring is displayed, in this case in a controller. Grails also automatically wires services to variables if they are declared with the same name as the service (without the first character capitalized). We’ll use the Groovy DSL for SQL here as well. It makes it incredibly easy to retrieve some data from the datasource.Here is the source for SomeController.groovy:

import groovy.sql.Sql
import grails.converters.XML

class CronacleController {

    def databaseService

    def index = { redirect(action:getSomeXML,params:params) }

    def getSomeXML = {
        def sql = Sql.newInstance(databaseService.getDataSource(databaseService.TEST))
        render sql.rows("select column from table") as XML
    }
}

Well, this concludes my first post on Grails/Groovy. I’m REALLY enjoying using both. It’s so much fun trying new things out without all the bloat usually surrounding it!Btw, I’m still not sure if I’m happy with the code for the service so don’t hesitate to comment :). I’m still in the early stages of learning Grails/Groovy so I’m sure things could be optimized.