Skip to content

Commit 5804ac2

Browse files
committed
JAVA-497: Use default ReadPreference for commands
1 parent baf0d8c commit 5804ac2

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/main/com/mongodb/DB.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public CommandResult command( DBObject cmd , int options, ReadPreference readPre
180180
*/
181181
public CommandResult command( DBObject cmd , int options )
182182
throws MongoException {
183-
return command(cmd, options, null);
183+
return command(cmd, options, getReadPreference());
184184
}
185185
/**
186186
* Executes a database command.

src/test/com/mongodb/DBTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb;
1818

19+
import java.net.UnknownHostException;
1920
import java.util.*;
2021

2122
import org.testng.annotations.Test;
@@ -109,4 +110,18 @@ public void testRenameAndDrop() throws MongoException {
109110
assertEquals( b.getName() , b2.getName() );
110111

111112
}
113+
114+
@Test
115+
public void testCommandToSecondary() throws MongoException, UnknownHostException {
116+
Mongo mongo = new Mongo(Arrays.asList(new ServerAddress("127.0.0.1"), new ServerAddress("127.0.0.1", 27018)));
117+
118+
if (isStandalone(mongo)) {
119+
return;
120+
}
121+
122+
DB db = mongo.getDB("secondaryTest");
123+
db.setReadPreference(ReadPreference.SECONDARY);
124+
CommandResult result = db.command("ping");
125+
assertEquals("127.0.0.1:27018", result.get("serverUsed"));
126+
}
112127
}

0 commit comments

Comments
 (0)