在关于mogodb的测试代码:
package com.test;
import java.net.UnknownHostException;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class TestDB {
public static String mongoStatServer = "192.168.0.1";
public static int mongoStatPort = 12345;
/**
* @param args
*/
public static void main(String[] args) {
Mongo mongo = null;
try {
mongo = new Mongo(mongoStatServer, mongoStatPort);
DB db = mongo.getDB("testdb");
//db.slaveOk();
System.out.println(db.getName()+"--"+db.getCollectionNames());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}
会出现如下异常:
com.mongodb.MongoException: not talking to master and retries used up
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:245)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:247)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:247)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:313)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:298)
at com.mongodb.DB.getCollectionNames(DB.java:298)
at com.test.TestDB.main(TestDB.java:24)
加上 db.slaveOk(); 就正常了.
比较奇怪的问题,.这里记录一下.