Skip to content

Commit d3065ba

Browse files
DATAMONGO-1158 - Add Support for mongo-java-driver 3.0
We now support mongo-java-driver version 2.x and 3.0 along with MongoDB Server 2.6.7 and 3.0.0. Pleaes note that some of the configurations options might no longer be valid when used with version 3 of the mongo-java-driver. Have a look at the table below so see some of the major differences in using version 2.x or 3.0. | 2.x | 3.0 ----------------------+-----------------------+----------------------------------------------- default WriteConcern | NONE | UNACKNOWLEDGED ----------------------+-----------------------+----------------------------------------------- option for slaveOk | available | ignored ----------------------+-----------------------+----------------------------------------------- option for autoConnect| available | ignored ----------------------+-----------------------+----------------------------------------------- write result checking | available | defaults WriteConncern to ACKNOWLEGED for EXCEPTION ----------------------+-----------------------+----------------------------------------------- rest index cache | available | throws UnsupportedOperationException ----------------------+-----------------------+----------------------------------------------- DBRef resolution | via DBRef.fetch | via collection.findOne ----------------------+-----------------------+----------------------------------------------- MapReduce Options | applied | ignored ----------------------+-----------------------+----------------------------------------------- authentication | via UserCredentials | via MongoClient ----------------------+-----------------------+----------------------------------------------- WriteConcernException | not available | translated to DataIntegretyViolationException ----------------------+-----------------------+----------------------------------------------- executeInSession | available | requestStart/requestDone commands ignored. ----------------------+-----------------------+----------------------------------------------- index creation | via createIndex | via createIndex ----------------------+-----------------------+-----------------------------------------------
1 parent d5456aa commit d3065ba

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
package org.springframework.data.mongodb.core;
1717

1818
import static com.mongodb.ReadPreference.*;
19+
import static org.springframework.data.mongodb.MongoClientVersion.*;
1920
import static org.springframework.data.mongodb.ReflectiveDbInvoker.*;
2021
import static org.springframework.data.mongodb.ReflectiveMapReduceInvoker.*;
2122
import static org.springframework.data.mongodb.ReflectiveWriteResultInvoker.*;
2223
import static org.springframework.data.mongodb.core.query.Criteria.*;
2324
import static org.springframework.data.mongodb.core.query.SerializationUtils.*;
25+
import static org.springframework.util.ObjectUtils.*;
2426

2527
import java.io.IOException;
2628
import java.util.ArrayList;
@@ -723,13 +725,22 @@ protected void prepareCollection(DBCollection collection) {
723725

724726
/**
725727
* Prepare the WriteConcern before any processing is done using it. This allows a convenient way to apply custom
726-
* settings in sub-classes.
728+
* settings in sub-classes. <br />
729+
* In case of using mongo-java-driver version 3 the returned {@link WriteConcern} will be defaulted to
730+
* {@link WriteConcern#ACKNOWLEDGED} when {@link WriteResultChecking} is set to {@link WriteResultChecking#EXCEPTION}.
727731
*
728732
* @param writeConcern any WriteConcern already configured or null
729733
* @return The prepared WriteConcern or null
730734
*/
731735
protected WriteConcern prepareWriteConcern(MongoAction mongoAction) {
732-
return writeConcernResolver.resolve(mongoAction);
736+
737+
WriteConcern wc = writeConcernResolver.resolve(mongoAction);
738+
739+
if (isMongo3Driver() && nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)
740+
&& (wc == null || wc.getW() < 1)) {
741+
return WriteConcern.ACKNOWLEDGED;
742+
}
743+
return wc;
733744
}
734745

735746
protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T> writer) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020
import static org.junit.Assume.*;
21-
import static org.springframework.data.mongodb.MongoClientVersion.*;
2221
import static org.springframework.data.mongodb.ReflectiveWriteConcernInvoker.*;
2322
import static org.springframework.data.mongodb.ReflectiveWriteResultInvoker.*;
2423
import static org.springframework.data.mongodb.core.query.Criteria.*;
@@ -227,11 +226,7 @@ public void bogusUpdateDoesNotTriggerException() throws Exception {
227226
public void throwsExceptionForDuplicateIds() {
228227

229228
MongoTemplate template = new MongoTemplate(factory);
230-
if (isMongo3Driver()) {
231-
template.setWriteConcern(WriteConcern.ACKNOWLEDGED);
232-
} else {
233-
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
234-
}
229+
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
235230

236231
Person person = new Person(new ObjectId(), "Amol");
237232
person.setAge(28);
@@ -254,11 +249,7 @@ public void throwsExceptionForDuplicateIds() {
254249
public void throwsExceptionForUpdateWithInvalidPushOperator() {
255250

256251
MongoTemplate template = new MongoTemplate(factory);
257-
if (isMongo3Driver()) {
258-
template.setWriteConcern(WriteConcern.ACKNOWLEDGED);
259-
} else {
260-
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
261-
}
252+
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
262253

263254
ObjectId id = new ObjectId();
264255
Person person = new Person(id, "Amol");
@@ -283,11 +274,7 @@ public void throwsExceptionForUpdateWithInvalidPushOperator() {
283274
public void throwsExceptionForIndexViolationIfConfigured() {
284275

285276
MongoTemplate template = new MongoTemplate(factory);
286-
if (isMongo3Driver()) {
287-
template.setWriteConcern(WriteConcern.ACKNOWLEDGED);
288-
} else {
289-
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
290-
}
277+
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
291278
template.indexOps(Person.class).ensureIndex(new Index().on("firstName", Direction.DESC).unique());
292279

293280
Person person = new Person(new ObjectId(), "Amol");
@@ -316,11 +303,7 @@ public void rejectsDuplicateIdInInsertAll() {
316303
thrown.expectMessage("E11000 duplicate key error index: database.person.$_id_");
317304

318305
MongoTemplate template = new MongoTemplate(factory);
319-
if (isMongo3Driver()) {
320-
template.setWriteConcern(WriteConcern.ACKNOWLEDGED);
321-
} else {
322-
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
323-
}
306+
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
324307

325308
ObjectId id = new ObjectId();
326309
Person person = new Person(id, "Amol");

0 commit comments

Comments
 (0)