Skip to content

Commit d6a0289

Browse files
committed
Changed the runCommand methods in MongoDatabase that don't take a ReadPreference 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
1 parent b345f51 commit d6a0289

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

driver-async/src/main/com/mongodb/async/client/MongoDatabase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ public interface MongoDatabase {
106106
<TDocument> MongoCollection<TDocument> getCollection(String collectionName, Class<TDocument> documentClass);
107107

108108
/**
109-
* Executes command in the context of the current database.
109+
* Executes the given command in the context of the current database with a read preference of {@link ReadPreference#primary()}.
110110
*
111111
* @param command the command to be run
112112
* @param callback the callback that is passed the command result
113113
*/
114114
void runCommand(Bson command, SingleResultCallback<Document> callback);
115115

116116
/**
117-
* Executes command in the context of the current database.
117+
* Executes the given command in the context of the current database with the given read preference.
118118
*
119119
* @param command the command to be run
120120
* @param readPreference the {@link com.mongodb.ReadPreference} to be used when executing the command
@@ -123,7 +123,7 @@ public interface MongoDatabase {
123123
void runCommand(Bson command, ReadPreference readPreference, SingleResultCallback<Document> callback);
124124

125125
/**
126-
* Executes command in the context of the current database.
126+
* Executes the given command in the context of the current database with a read preference of {@link ReadPreference#primary()}.
127127
*
128128
* @param command the command to be run
129129
* @param resultClass the default class to cast any documents returned from the database into.
@@ -133,7 +133,7 @@ public interface MongoDatabase {
133133
<TResult> void runCommand(Bson command, Class<TResult> resultClass, SingleResultCallback<TResult> callback);
134134

135135
/**
136-
* Executes command in the context of the current database.
136+
* Executes the given command in the context of the current database with the given read preference.
137137
*
138138
* @param command the command to be run
139139
* @param readPreference the {@link com.mongodb.ReadPreference} to be used when executing the command

driver-async/src/main/com/mongodb/async/client/MongoDatabaseImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.mongodb.client.model.CreateCollectionOptions;
2525
import com.mongodb.operation.AsyncOperationExecutor;
2626
import com.mongodb.operation.CommandReadOperation;
27-
import com.mongodb.operation.CommandWriteOperation;
2827
import com.mongodb.operation.CreateCollectionOperation;
2928
import com.mongodb.operation.DropDatabaseOperation;
3029
import org.bson.BsonDocument;
@@ -132,7 +131,7 @@ public void runCommand(final Bson command, final ReadPreference readPreference,
132131
public <TResult> void runCommand(final Bson command, final Class<TResult> resultClass,
133132
final SingleResultCallback<TResult> callback) {
134133
notNull("command", command);
135-
executor.execute(new CommandWriteOperation<TResult>(getName(), toBsonDocument(command), codecRegistry.get(resultClass)), callback);
134+
runCommand(command, ReadPreference.primary(), resultClass, callback);
136135
}
137136

138137
@Override

driver-async/src/test/unit/com/mongodb/async/client/MongoDatabaseSpecification.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import com.mongodb.WriteConcern
2121
import com.mongodb.async.FutureResultCallback
2222
import com.mongodb.client.model.CreateCollectionOptions
2323
import com.mongodb.operation.CommandReadOperation
24-
import com.mongodb.operation.CommandWriteOperation
2524
import com.mongodb.operation.CreateCollectionOperation
2625
import com.mongodb.operation.DropDatabaseOperation
2726
import org.bson.BsonDocument
@@ -107,10 +106,11 @@ class MongoDatabaseSpecification extends Specification {
107106
futureResultCallback.get()
108107

109108
then:
110-
def operation = executor.getWriteOperation() as CommandWriteOperation<Document>
109+
def operation = executor.getReadOperation() as CommandReadOperation<Document>
111110

112111
then:
113112
operation.command == command
113+
executor.getReadPreference() == primary()
114114

115115
when:
116116
futureResultCallback = new FutureResultCallback<Document>()
@@ -125,11 +125,12 @@ class MongoDatabaseSpecification extends Specification {
125125
when:
126126
futureResultCallback = new FutureResultCallback<BsonDocument>()
127127
database.runCommand(command, BsonDocument, futureResultCallback)
128-
operation = executor.getWriteOperation() as CommandWriteOperation<BsonDocument>
128+
operation = executor.getReadOperation() as CommandReadOperation<BsonDocument>
129129
futureResultCallback.get()
130130

131131
then:
132132
operation.command == command
133+
executor.getReadPreference() == primary()
133134

134135
when:
135136
futureResultCallback = new FutureResultCallback<BsonDocument>()

driver/src/main/com/mongodb/MongoDatabaseImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.mongodb.client.MongoIterable;
2323
import com.mongodb.client.model.CreateCollectionOptions;
2424
import com.mongodb.operation.CommandReadOperation;
25-
import com.mongodb.operation.CommandWriteOperation;
2625
import com.mongodb.operation.CreateCollectionOperation;
2726
import com.mongodb.operation.DropDatabaseOperation;
2827
import com.mongodb.operation.OperationExecutor;
@@ -108,7 +107,7 @@ public Document runCommand(final Bson command, final ReadPreference readPreferen
108107

109108
@Override
110109
public <TResult> TResult runCommand(final Bson command, final Class<TResult> resultClass) {
111-
return executor.execute(new CommandWriteOperation<TResult>(getName(), toBsonDocument(command), codecRegistry.get(resultClass)));
110+
return runCommand(command, ReadPreference.primary(), resultClass);
112111
}
113112

114113
@Override

driver/src/main/com/mongodb/client/MongoDatabase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ public interface MongoDatabase {
105105
<TDocument> MongoCollection<TDocument> getCollection(String collectionName, Class<TDocument> documentClass);
106106

107107
/**
108-
* Executes command in the context of the current database.
108+
* Executes the given command in the context of the current database with a read preference of {@link ReadPreference#primary()}.
109109
*
110110
* @param command the command to be run
111111
* @return the command result
112112
*/
113113
Document runCommand(Bson command);
114114

115115
/**
116-
* Executes command in the context of the current database.
116+
* Executes the given command in the context of the current database with the given read preference.
117117
*
118118
* @param command the command to be run
119119
* @param readPreference the {@link ReadPreference} to be used when executing the command
@@ -122,7 +122,7 @@ public interface MongoDatabase {
122122
Document runCommand(Bson command, ReadPreference readPreference);
123123

124124
/**
125-
* Executes command in the context of the current database.
125+
* Executes the given command in the context of the current database with a read preference of {@link ReadPreference#primary()}.
126126
*
127127
* @param command the command to be run
128128
* @param resultClass the default class to cast any documents returned from the database into.
@@ -132,7 +132,7 @@ public interface MongoDatabase {
132132
<TResult> TResult runCommand(Bson command, Class<TResult> resultClass);
133133

134134
/**
135-
* Executes command in the context of the current database.
135+
* Executes the given command in the context of the current database with the given read preference.
136136
*
137137
* @param command the command to be run
138138
* @param readPreference the {@link ReadPreference} to be used when executing the command

driver/src/test/unit/com/mongodb/MongoDatabaseSpecification.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package com.mongodb
1818

1919
import com.mongodb.client.model.CreateCollectionOptions
2020
import com.mongodb.operation.CommandReadOperation
21-
import com.mongodb.operation.CommandWriteOperation
2221
import com.mongodb.operation.CreateCollectionOperation
2322
import com.mongodb.operation.DropDatabaseOperation
2423
import org.bson.BsonDocument
@@ -102,10 +101,11 @@ class MongoDatabaseSpecification extends Specification {
102101

103102
when:
104103
database.runCommand(command)
105-
def operation = executor.getWriteOperation() as CommandWriteOperation<Document>
104+
def operation = executor.getReadOperation() as CommandReadOperation<Document>
106105

107106
then:
108107
operation.command == command
108+
executor.getReadPreference() == primary()
109109

110110
when:
111111
database.runCommand(command, primaryPreferred())
@@ -117,10 +117,11 @@ class MongoDatabaseSpecification extends Specification {
117117

118118
when:
119119
database.runCommand(command, BsonDocument)
120-
operation = executor.getWriteOperation() as CommandWriteOperation<BsonDocument>
120+
operation = executor.getReadOperation() as CommandReadOperation<BsonDocument>
121121

122122
then:
123123
operation.command == command
124+
executor.getReadPreference() == primary()
124125

125126
when:
126127
database.runCommand(command, primaryPreferred(), BsonDocument)

0 commit comments

Comments
 (0)