简体   繁体   中英

Grails 3 - GORM for MongoDB - nearest read for replica set

I'm upgrading my Grails 2.4 web application to Grails 3, and I'm considering switching from my custom DAO to GORM for my Mongo database.

I'm trying to understand how to setup GORM correctly, in particular about connection options , but its documentation is slightly misleading to me. The Advanced Configuration ("Mongo Database Connection Configuration") states

Available options and their descriptions are defined in the MongoOptions javadoc.

so I'm tempted to assume that I'm allowed to use any of those options.

But later on in the same section (Configuration Options Guide) I read

Below is a complete example showing all configuration options:

showing only 9 options.

My issue is 'converting' my custom DAO

MongoClientOptions options = new MongoClientOptions .Builder()
                                                    .connectionsPerHost(1000)
                                                    .threadsAllowedToBlockForConnectionMultiplier(5)
                                                    .maxWaitTime(4000)
                                                    .socketTimeout(2000).build();
List<ServerAddress> list = getMongoReplicaSet();
mongo = new MongoClient(list, options);
mongo.setReadPreference(ReadPreference.nearest());

to an equivalent configuration

grails {
    mongodb {
        options {
            connectionsPerHost = 1000
            threadsAllowedToBlockForConnectionMultiplier = 5
            maxWaitTime = 4000
            socketTimeout = 2000
        }
    }
}

but how to define the read preference? Am I allowed to do something like this?

grails {
    mongodb {
        options {
            readPreference = com.mongodb.ReadPreference.nearest()
        }
    }
}

Thanks in advance!

Yes you can set anything in the MongoClientOptions.Builder class via configuration. Although you syntax is wrong, it should be:

grails {
   mongodb {
      options {
        readPreference = com.mongodb.ReadPreference.nearest()
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM