(Quick Reference)

6 Multiple Data Sources

Version: 2023.3.0

6 Multiple Data Sources

GORM for MongoDB supports the notion of multiple data sources where multiple individual MongoClient instances can be configured and switched between.

6.1 Configuring Multiple Mongo Clients

To configure multiple Mongo client connections you need to use the grails.mongodb.connections setting. For example in application.yml:

app/conf/application.yml
grails:
    mongodb:
        url: mongodb://localhost/books
        connections:
            moreBooks:
                url: mongodb://localhost/moreBooks
            evenMoreBooks:
                url: mongodb://localhost/moreBooks

You can configure individual settings for each Mongo client. If a setting is not specified by default the setting is inherited from the default Mongo client.

6.2 Mapping Domain Classes to Mongo Clients

If a domain class has no specify Mongo client connection configuration then the default is used.

You can set the connection method in the mapping block to configure an alternate Mongo Client.

For example, if you want to use the ZipCode domain to use a Mongo client connection called 'lookup', configure it like this:

class ZipCode {

   String code

   static mapping = {
      connection 'lookup'
   }
}

A domain class can also use two or more configured Mongo client connections by using the connections method with a list of names to configure more than one, for example:

class ZipCode {

   String code

   static mapping = {
      connections(['lookup', 'auditing'])
   }
}

If a domain class uses the default connection and one or more others, you can use the ConnectionSource.DEFAULT constant to indicate that:

import org.grails.datastore.mapping.core.connections.*

class ZipCode {

   String code

   static mapping = {
      connections(['lookup', ConnectionSource.DEFAULT])
   }
}

If a domain class uses all configured DataSource instances use the value ALL:

import org.grails.datastore.mapping.core.connections.*

class ZipCode {

   String code

   static mapping = {
      connection ConnectionSource.ALL
   }
}

6.3 Switching between Mongo Clients

You can switch to a different connection at runtime with the withConnection method:

Book.withConnection("moreBooks") {
    Book.list()
}

Any logic executed within the body of the closure will use the alternate connection. Once the close finishes execution GORM will switch back to the default connection automatically.

6.4 The ConnectionSources API

Introduced in GORM 6.0, the ConnectionSources API allows you to introspect the data sources configured for the application:

@Autowired
MongoDatastore mongoDatastore
...
ConnectionSources<MongoClient, MongoConnectionSourceSettings> connectionSources
                                        = mongoDatastore.getConnectionSources()

for(ConnectionSource<MongoClient, MongoConnectionSourceSettings> connectionSource in connectionSources) {
        println "Name $connectionSource.name"
        MongoClient mongoClient = connectionSource.source
}