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
Feature Flags in Terraform
Feature flagging any code can be useful to developers but many don’t know how to or even that you can do it in Terraform. Some benefits of Feature Flagging your code You can enable different […]
Infrastructure as Code – The Wrong Way
You are probably familiar with the term “infrastructure as code”. It’s a great concept, and it’s gaining steam in the industry. Unfortunately, just as we had a lot to learn about how to write clean […]
Snowflake CI/CD using Jenkins and Schemachange
CI/CD and Management of Data Warehouses can be a serious challenge. In this blog you will learn how to setup CI/CD for Snowflake using Schemachange, Github, and Jenkins. For access to the code check out […]
How to get your pull requests approved more quickly
TL;DR The fewer reviews necessary, the quicker your PR gets approved. Code reviews serve an essential function on any software codebase. Done right, they help ensure correctness, reliability, and maintainability of code. On many teams, […]