« Back to home

Re-using common properties when defining multiple datasources in Grails

Posted on

tl;dr:

Avoiding code duplication when defining multiple datasources in Grails.

Long version

One of my Grails applications has to connect to seven database instances that share many characteristics like driver, dialect, username and password which were all copy pasted in my first version.

Example:

As you can see only the url parameter changes while the rest stays static and is wasted text.

Shorter version

To make it more compact and re-use the common part I found that you can define a closure which can be used to initialise each datasource entry.

As you can see I also included the url which I will override after the initialisation.

Using the closure this leaves us with the following version.

The final version can be download here.