5

I want to set the WriteConcern in spring mongodb to acknowledged. Also, I'm wondering if this is the default value? I am using the spring.data.mongodb.uri in my application.properties so I don't have any mongo configuration class.

demig0d
  • 1,131
  • 1
  • 12
  • 24

4 Answers4

3

From the docs of SpringData here

9.4.3. 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.

9.4.4. 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);
}

Find a direct implementation here

Rbk
  • 72
  • 1
  • 11
3

you can do this over Spring bean

@Configuration
public class MongoConfiguration {

@Bean
public WriteConcernResolver writeConcernResolver() {
    return action -> {
        System.out.println("Using Write Concern of Acknowledged");
        return WriteConcern.ACKNOWLEDGED;
    };
}

}
panser
  • 1,949
  • 22
  • 16
3

It is not sufficient to only provide a WriteConcernResolver over Bean configuration. The MongoTemplate will not use it. To make this happen you have to create a class like this with two options to set the WriteConcern:

import com.mongodb.WriteConcern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.WriteResultChecking;
import org.springframework.data.mongodb.core.convert.MongoConverter;

@Configuration
public class MongoConfiguration {
    Logger logger = LoggerFactory.getLogger(MongoConfiguration.class);

    public MongoConfiguration() {
        logger.info("MongoConfiguration applied  ...");
    }

    @Bean
    MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDbFactory, MongoConverter converter) {
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter);+
        // Version 1: set statically
        logger.debug("Setting WriteConcern statically to ACKNOWLEDGED");
        mongoTemplate.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        // Version 2: provide a WriteConcernResolver, which is called for _every_ MongoAction
        // which might degrade performance slightly (not tested)
        // and is very flexible to determine the value
        mongoTemplate.setWriteConcernResolver(action -> {
            logger.debug("Action {} called on collection {} for document {} with WriteConcern.MAJORITY. Default WriteConcern was {}", action.getMongoActionOperation(), action.getCollectionName(), action.getDocument(), action.getDefaultWriteConcern());
            return WriteConcern.ACKNOWLEDGED;
        });
        mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
        return mongoTemplate;
    }
}
1

You can set the write-concern in the xml-configuration (if applicable)

<mongo:db-factory ... write-concern="SAFE"/>
Matthias M
  • 12,906
  • 17
  • 87
  • 116