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

Object Partners profile.

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 to Wayne Vetrone Cancel reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]