Skip to content

Commit

Permalink
set WriteConcern and ReadPreference for MongoTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
apriltuesday committed Jun 19, 2024
1 parent 203b559 commit 1be9e49
Showing 1 changed file with 4 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;

import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -91,6 +93,8 @@ public MongoTemplate mongoTemplate(@Qualifier("primaryFactory") MongoDbFactory m
MappingMongoConverter converter) {
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter);
mongoTemplate.setWriteConcern(WriteConcern.MAJORITY);
mongoTemplate.setReadPreference(ReadPreference.valueOf(readPreference));
mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
return mongoTemplate;
}
Expand Down

0 comments on commit 1be9e49

Please sign in to comment.