From 2fcc323bcd0b19cc3b8a7cf6be8fc7064829995c Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Wed, 16 Nov 2011 14:55:35 -0500 Subject: [PATCH] DATAMONGO-213 - Add WriteConcern to arguments of MongoOperations.update*() methods --- src/docbkx/reference/mongodb.xml | 71 ++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 18 deletions(-) diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index 02a702f50..55670b419 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -770,7 +770,7 @@ public class MongoConfiguration { MongoTemplate in the context of the Spring container. -
+
Instantiating MongoTemplate You can use Java to create and register an instance of @@ -840,8 +840,8 @@ public class AppConfig { Other optional properties that you might like to set when creating a MongoTemplate are the default WriteResultCheckingPolicy, - WriteConcern, and SlaveOk - write option. + WriteConcern, and + ReadPreference. The preferred way to reference the operations on @@ -849,7 +849,7 @@ public class AppConfig { MongoOperations. -
+
WriteResultChecking Policy When in development it is very handy to either log or throw an @@ -864,24 +864,59 @@ public class AppConfig { use a WriteResultChecking value of NONE.
-
+
WriteConcern You can set the com.mongodb.WriteConcern property that the MongoTemplate will use for write operations if it has not yet been specified via the driver at a - higher level such as com.mongodb.Mongo. If MongoTemplate's - WriteConcern property is not set it will - default to the one set in the MongoDB driver's DB or Collection - setting. - - - Setting the WriteConcern to different - values when saving an object will be provided in a future release. - This will most likely be handled using mapping metadata provided - either in the form of annotations on the domain object or by an - external fluent DSL. - + higher level such as com.mongodb.Mongo. If + MongoTemplate's WriteConcern property is not + set it will default to the one set in the MongoDB driver's DB or + Collection setting. +
+ +
+ WriteConcernResolver + + For more advanced cases where you want to set different + WriteConcern values on a per-operation basis + (for remove, update, insert and save operations), a strategy interface + called WriteConcernResolver can be + configured on MongoTemplate. Since + MongoTemplate is used to persist POJOs, the + WriteConcernResolver lets you create a + policy that can map a specific POJO class to a + WriteConcern value. The + WriteConcernResolver interface is shown + below. + + public interface WriteConcernResolver { + WriteConcern resolve(MongoAction action); +} + + The passed in argument, MongoAction, is what you use to + determine the WriteConcern value to be used or + to use the value of the Template itself as a default. + MongoAction contains the collection name being + written to, the java.lang.Class of the POJO, + the converted DBObject, as well as the + operation as an enumeration + (MongoActionOperation: REMOVE, UPDATE, INSERT, + INSERT_LIST, SAVE) and a few other pieces of contextual information. + For example, + + private class MyAppWriteConcernResolver implements WriteConcernResolver { + + public WriteConcern resolve(MongoAction action) { + if (action.getEntityClass().getSimpleName().contains("Audit")) { + return WriteConcern.NONE; + } else if (action.getEntityClass().getSimpleName().contains("Metadata")) { + return WriteConcern.JOURNAL_SAFE; + } + return action.getDefaultWriteConcern(); + } + }
@@ -1979,7 +2014,7 @@ MapReduceResults<ValueObject> results = mongoOperations.mapReduce(query, " so it may feel more approachable vs. using Map-Reduce. Using the group operations does have some limitations, for example it is not supported in a shareded environment and it returns the full result set in a single BSON - object, so the result should be small, less than 10,000 keys. + object, so the result should be small, less than 10,000 keys. Spring provides integration with MongoDB's group operation by providing methods on MongoOperations to simplify the creation and