Using secondary datasources in Grails 3

Something changed in Grails 3 and how datasources are configured.

If you have datasources defined like this:

   dataSources:
       dataSource:
           pooled: true
           jmxExport: true
           driverClassName: org.h2.Driver
           username: sa
           password:
           dbCreate: create-drop
           url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
       secondary:
           pooled: true
           jmxExport: true
           driverClassName: org.h2.Driver
           username: sa
           password:
           dbCreate: create-drop
           url: jdbc:h2:mem:devDb2;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE

 

You can use the “other datasource” (secondary) in a Grails domain class very simply:

   class Book {
   
       String title
   
       static constraints = {
       }
   
       static mapping = {
           datasource 'secondary'
       }
   }
   

And you can use the dataSource object by name in a Grails service like this:

   class MyService {
   
      def dataSource
   .....
   }
   

So you would think that the following would work:

   class MyService {
   
      def secondary
   .....
   }

but it doesn’t… Grails doesn’t know how to wire that secondary datasource so it’s null.

Instead, you have to put it in the resources.groovy or use Spring’s @Autowired on it like so:

   class BookSqlSecondaryService {
   
      @Autowired
      @Qualifier('dataSource_secondary')
      def secondary
   }
   

The documentation will be updated in Grails 3.2 but it’s still the case in Grails 3.0.x and Grails 3.1.x.

About the Author

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

One thought on “Using secondary datasources in Grails 3

  1. Wayne Vetrone says:

    I am unable to use Qualifier in my Grails 3.1.4 project. Any suggestions?

    startup failed:
    C:\gcprime_local\gcprime-3\gcprime\grails-app\services\adminrequest\BasicReports
    Service.groovy: 11: unable to resolve class Qualifier , unable to find class fo
    r annotation
    @ line 11, column 1.
    @Qualifier(‘dataSource_workflow’)
    ^

    I have added the following imports and that did not help

    import org.springframework.beans.factory.annotation.Autowired
    import org.springframework.beans.factory.annotation.Qualifier

    1. Dave — I hadn’t used this in Grails 3.1 before (I was in the 3.0 world)… AL (below ) said it worked in Grails 3.1.8… so maybe upgrade your Grails version?

  2. Al H. says:

    Thank you very much Mike! After a couple hours trying to figure out how to get this done in Grails 3, your advice did the trick. Note that I’m using Grails 3.1.8, so I didn’t run into the issue that Wayne did in the previous post.

  3. Fernando Andrauss says:

    Thank you very much! Saved my work.

Leave a Reply

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

Related Blog Posts
Why we started using JSON Schema in our internal Helm charts
Helm 3 supports validating the provided chart values against a JSON Schema. While it may be quicker to get started in your chart development without a schema, we found it valuable for a number of […]
Rewriting files in Google Cloud Storage
Rewriting Files in GCP Note: even though this code is in Python, this should be the same idea in JavaScript, Go, etc. I wrote the following to copy a file from one Google Cloud Storage […]
Interpreting Spatial Data in the Age of COVID-19
As 2020 has come to an end, many are eager to leave the mess of COVID-19 behind with the new year and gain a fresh start. Unfortunately, new cases are still soaring across the United […]
Building a Better Mousetrap
Recently, my daughter (age 6) was into building “mousetraps” out of shoe boxes. These were more or less comfortable cardboard mouse houses complete with beds, rooms, everything a mouse could want or need and not […]