Sep 24, 2013

Rollback Multiple Datasources in Grails Integration Tests

Grails GORM has solid support for using multiple datasources in both 1.3.x with the Datasources plugin, and 2.x with built-in multi-datasource support. This feature allows you to partition your Domain classes and Services to attach to two or more databases. One pitfall you’ll encounter though, is that Integration Tests of the secondary datasources DO NOT rollback their transactions on versions of Grails < 2.3 (GRAILS-9771). However, we can borrow the approach from the 2.3 patch to fix our pre 2.3 test classes!

The approach is simply to add rollback steps for the autowired transactionManager in the JUnit test setUp() and tearDown() methods (or Spock setup() and cleanup()). In this example I have a datasource named “db2” so the injected name of the bean is transactionManager_db2. The test then gets a reference to the transactionStatus in setUp() and rolls it back in tearDown(). (Note: this code was written for a Grails 1.3 app but should also work in 2.x)

Of course the downfall of this approach is that you must remember to setup each test class for a non-default datasource (or use a base test class hierarchy). But this code is essential for integration testing your secondary databases until the app has been migrated to Grails 2.3!

As a reference, here is the configuration I was using for the Datasources plugin in Grails 1.3:

About the Author

Jeff Sheets profile.

Jeff Sheets

VP - Technology

Jeff has developed Java, Groovy, Grails, and Javascript web apps for industries as varied as Defense, Energy, Weather, Insurance, and Telecom. He is a co-organizer of the Omaha Java Users Group. Jeff has worked on Grails projects since the Grails 1.3.x days, and has experience with production Groovy code as well as Spock tests and Gradle builds. His latest focus has been on AngularJS and Spring Boot applications using JHipster. Jeff also enjoys volunteering at local CoderDojo events to teach programming to our next generation.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Android Development for iOS Developers
Android development has greatly improved since the early days. Maybe you tried it out when Android development was done in Eclipse, emulators were slow and buggy, and Java was the required language. Things have changed […]
Add a custom object to your Liquibase diff
Adding a custom object to your liquibase diff is a pretty simple two step process. Create an implementation of DatabaseObject Create an implementation of SnapshotGenerator In my case I wanted to add tracking of Stored […]
Keeping Secrets Out of Terraform State
There are many instances where you will want to create resources via Terraform with secrets that you just don’t want anyone to see. These could be IAM credentials, certificates, RDS DB credentials, etc. One problem […]
Validating Terraform Plans using Open Policy Agent
When developing infrastructure as code using terraform, it can be difficult to test and validate changes without executing the code against a real environment. The feedback loop between writing a line of code and understanding […]