Setup multiple data sources in Grails 2.0


Hi there,

lately I was trying to use multiple data sources with a Grails application. It took me some time to figure out how to do that, therefore I thought it would be something worth sharing. The approach described here will only work for applications written in Grails 2.0 and above since the usage of multiple data sources is a feature which was added into Grails starting with version 2.0.

So here is how you do it. Unsurprisingly you can add your additional data sources within your DataSources.groovy configuration file. You simply add them to the appropriate subsection (development, test, production etc.) within the environments section of your configuration file. That should look some like this:

environments {
    development {
        dataSource {
        //This is your DEFAULT data source
	//Add the configuration parameters for your DEFAULT data source here.
        }

	dataSource_monsters {
	//This is thefirst of your additional data sources, put the configuration
        //parameters  corresponding to the first data source here.
        }

	dataSource_aliens {
	//This is the second of your additional data sources, put the configuration
        //parameters  corresponding to the second data source here.
        }
    }
    test {
	//You can specify other data sources in your test environment as usual.
        dataSource {

	}
	dataSource_monsters {

        }
	dataSource_aliens {

        }
    }
    production {
	//You can specify other data sources in your production environment as usual.
        dataSource {

	}
	dataSource_monsters {

        }
	dataSource_aliens {

        }
    }
}

As you may have noticed, there is a convention on how to specify additional data sources. The convention is to name the section “datasource_[your choosen name]”. So in the example file there are two different data sources, “datasource_monsters” and “datasource_aliens”. The name you choose after the the underscore is totally up to you. Another thing to notice is that you specify your default data source for a specific environment by using the “dataSource” clause without the additional underscore part.

So for each environment – development, test and production we have defined three different data sources. One default data source, and two additional data sources (‘monsters’ and ‘aliens’).

But how do you decide which data source a specific class should use?

The answer is by defining a mapping section within your class. Let’s see how that looks with some domain classes:

class Lair{

}

class Monster{
    static mapping = {
        datasource 'monsters'
    }
}

class Alien{
    static mapping = {
        datasource 'aliens'
    }
}

If a class does not specify a mapping section it will use the default data source defined in your section. So the Lair class would use the default data source for persisting itself. The other two classes (‘Monster’ and ‘Alien’) each define a mapping section with a data source that they would like to use. So the Monster class would be mapped to a monsters table in the ‘monsters’ data source and the Aliens class would be mapped to an aliens table in the ‘aliens’ data source.

I hope you enjoyed the read an everything worked for you. Drop me a comment if you experience any issues or if you think I should add some information to this article.

Advertisements

2 comments

  1. Do your integration tests work with multiple datasources?

    I found that the database doesn’t get cleaned up after each test when my domain class point to a non-default datasource. 😦

    Still trying to figure out a solution.

  2. Pingback: Grails 2 – using multiple DataSources | Knights of the Virtual Machine


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s