Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Changed the runCommand methods in MongoDatabase that don't take a Rea…

…dPreference to use ReadPreference.primary() instead of treating the command as a write operation.

This is a subtle distinction, but an important one, as a primary read preference will allow the command to be sent to an uninitialized member of a replica set
that the client has directly connected to, whereas a write operation will only allow the command to be sent to a primary member of a replica set.

 JAVA-1867
commit 0a28ed5a9b2a55ad3723b33e641ec5598562dc56 1 parent 8c5a20d
@jyemin jyemin authored
View
8 driver-async/src/main/com/mongodb/async/client/MongoDatabase.java
@@ -106,7 +106,7 @@
<TDocument> MongoCollection<TDocument> getCollection(String collectionName, Class<TDocument> documentClass);
/**
- * Executes command in the context of the current database.
+ * Executes the given command in the context of the current database with a read preference of {@link ReadPreference#primary()}.
*
* @param command the command to be run
* @param callback the callback that is passed the command result
@@ -114,7 +114,7 @@
void runCommand(Bson command, SingleResultCallback<Document> callback);
/**
- * Executes command in the context of the current database.
+ * Executes the given command in the context of the current database with the given read preference.
*
* @param command the command to be run
* @param readPreference the {@link com.mongodb.ReadPreference} to be used when executing the command
@@ -123,7 +123,7 @@
void runCommand(Bson command, ReadPreference readPreference, SingleResultCallback<Document> callback);
/**
- * Executes command in the context of the current database.
+ * Executes the given command in the context of the current database with a read preference of {@link ReadPreference#primary()}.
*
* @param command the command to be run
* @param resultClass the default class to cast any documents returned from the database into.
@@ -133,7 +133,7 @@
<TResult> void runCommand(Bson command, Class<TResult> resultClass, SingleResultCallback<TResult> callback);
/**
- * Executes command in the context of the current database.
+ * Executes the given command in the context of the current database with the given read preference.
*
* @param command the command to be run
* @param readPreference the {@link com.mongodb.ReadPreference} to be used when executing the command
View
3  driver-async/src/main/com/mongodb/async/client/MongoDatabaseImpl.java
@@ -24,7 +24,6 @@
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.operation.AsyncOperationExecutor;
import com.mongodb.operation.CommandReadOperation;
-import com.mongodb.operation.CommandWriteOperation;
import com.mongodb.operation.CreateCollectionOperation;
import com.mongodb.operation.DropDatabaseOperation;
import org.bson.BsonDocument;
@@ -131,7 +130,7 @@ public void runCommand(final Bson command, final ReadPreference readPreference,
public <TResult> void runCommand(final Bson command, final Class<TResult> resultClass,
final SingleResultCallback<TResult> callback) {
notNull("command", command);
- executor.execute(new CommandWriteOperation<TResult>(getName(), toBsonDocument(command), codecRegistry.get(resultClass)), callback);
+ runCommand(command, ReadPreference.primary(), resultClass, callback);
}
@Override
View
7 driver-async/src/test/unit/com/mongodb/async/client/MongoDatabaseSpecification.groovy
@@ -21,7 +21,6 @@ import com.mongodb.WriteConcern
import com.mongodb.async.FutureResultCallback
import com.mongodb.client.model.CreateCollectionOptions
import com.mongodb.operation.CommandReadOperation
-import com.mongodb.operation.CommandWriteOperation
import com.mongodb.operation.CreateCollectionOperation
import com.mongodb.operation.DropDatabaseOperation
import org.bson.BsonDocument
@@ -107,10 +106,11 @@ class MongoDatabaseSpecification extends Specification {
futureResultCallback.get()
then:
- def operation = executor.getWriteOperation() as CommandWriteOperation<Document>
+ def operation = executor.getReadOperation() as CommandReadOperation<Document>
then:
operation.command == command
+ executor.getReadPreference() == primary()
when:
futureResultCallback = new FutureResultCallback<Document>()
@@ -125,11 +125,12 @@ class MongoDatabaseSpecification extends Specification {
when:
futureResultCallback = new FutureResultCallback<BsonDocument>()
database.runCommand(command, BsonDocument, futureResultCallback)
- operation = executor.getWriteOperation() as CommandWriteOperation<BsonDocument>
+ operation = executor.getReadOperation() as CommandReadOperation<BsonDocument>
futureResultCallback.get()
then:
operation.command == command
+ executor.getReadPreference() == primary()
when:
futureResultCallback = new FutureResultCallback<BsonDocument>()
View
3  driver/src/main/com/mongodb/MongoDatabaseImpl.java
@@ -22,7 +22,6 @@
import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.operation.CommandReadOperation;
-import com.mongodb.operation.CommandWriteOperation;
import com.mongodb.operation.CreateCollectionOperation;
import com.mongodb.operation.DropDatabaseOperation;
import com.mongodb.operation.OperationExecutor;
@@ -108,7 +107,7 @@ public Document runCommand(final Bson command, final ReadPreference readPreferen
@Override
public <TResult> TResult runCommand(final Bson command, final Class<TResult> resultClass) {
- return executor.execute(new CommandWriteOperation<TResult>(getName(), toBsonDocument(command), codecRegistry.get(resultClass)));
+ return runCommand(command, ReadPreference.primary(), resultClass);
}
@Override
View
8 driver/src/main/com/mongodb/client/MongoDatabase.java
@@ -105,7 +105,7 @@
<TDocument> MongoCollection<TDocument> getCollection(String collectionName, Class<TDocument> documentClass);
/**
- * Executes command in the context of the current database.
+ * Executes the given command in the context of the current database with a read preference of {@link ReadPreference#primary()}.
*
* @param command the command to be run
* @return the command result
@@ -113,7 +113,7 @@
Document runCommand(Bson command);
/**
- * Executes command in the context of the current database.
+ * Executes the given command in the context of the current database with the given read preference.
*
* @param command the command to be run
* @param readPreference the {@link ReadPreference} to be used when executing the command
@@ -122,7 +122,7 @@
Document runCommand(Bson command, ReadPreference readPreference);
/**
- * Executes command in the context of the current database.
+ * Executes the given command in the context of the current database with a read preference of {@link ReadPreference#primary()}.
*
* @param command the command to be run
* @param resultClass the default class to cast any documents returned from the database into.
@@ -132,7 +132,7 @@
<TResult> TResult runCommand(Bson command, Class<TResult> resultClass);
/**
- * Executes command in the context of the current database.
+ * Executes the given command in the context of the current database with the given read preference.
*
* @param command the command to be run
* @param readPreference the {@link ReadPreference} to be used when executing the command
View
7 driver/src/test/unit/com/mongodb/MongoDatabaseSpecification.groovy
@@ -18,7 +18,6 @@ package com.mongodb
import com.mongodb.client.model.CreateCollectionOptions
import com.mongodb.operation.CommandReadOperation
-import com.mongodb.operation.CommandWriteOperation
import com.mongodb.operation.CreateCollectionOperation
import com.mongodb.operation.DropDatabaseOperation
import org.bson.BsonDocument
@@ -102,10 +101,11 @@ class MongoDatabaseSpecification extends Specification {
when:
database.runCommand(command)
- def operation = executor.getWriteOperation() as CommandWriteOperation<Document>
+ def operation = executor.getReadOperation() as CommandReadOperation<Document>
then:
operation.command == command
+ executor.getReadPreference() == primary()
when:
database.runCommand(command, primaryPreferred())
@@ -117,10 +117,11 @@ class MongoDatabaseSpecification extends Specification {
when:
database.runCommand(command, BsonDocument)
- operation = executor.getWriteOperation() as CommandWriteOperation<BsonDocument>
+ operation = executor.getReadOperation() as CommandReadOperation<BsonDocument>
then:
operation.command == command
+ executor.getReadPreference() == primary()
when:
database.runCommand(command, primaryPreferred(), BsonDocument)
Please sign in to comment.
Something went wrong with that request. Please try again.