在mongodb中执行isMaster命令的异常

时间:2012-10-17 11:27:46

标签: java mongodb

我试图从部署在JBOSS中的应用程序写入mongodb,这就是我得到的,我在mongodb中需要做些什么吗?它似乎是论坛讨论中的并发问题。

14:00:07,083 WARNING [com.mongodb.tcp] (Camel (camel-2) thread #3 - JmsConsumer[barTopic]) Exception executing isMaster command on /127.0.0.1:27017: java.lang.NullPointerException
        at com.mongodb.DBPort._open(DBPort.java:203) [mongo-java-driver-2.9.1.jar:]
        at com.mongodb.DBPort.go(DBPort.java:107) [mongo-java-driver-2.9.1.jar:]
        at com.mongodb.DBPort.go(DBPort.java:88) [mongo-java-driver-2.9.1.jar:]
        at com.mongodb.DBPort.findOne(DBPort.java:143) [mongo-java-driver-2.9.1.jar:]
        at com.mongodb.DBPort.runCommand(DBPort.java:148) [mongo-java-driver-2.9.1.jar:]
        at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:539) [mongo-java-driver-2.9.1.jar:]
        at com.mongodb.Mongo.getMaxBsonObjectSize(Mongo.java:600) [mongo-java-driver-2.9.1.jar:]
        at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:233) [mongo-java-driver-2.9.1.jar:]
        at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:205) [mongo-java-driver-2.9.1.jar:]
        at com.mongodb.DBCollection.insert(DBCollection.java:57) [mongo-java-driver-2.9.1.jar:]
        at com.mongodb.DBCollection.insert(DBCollection.java:100) [mongo-java-driver-2.9.1.jar:]
        at com.magick.magicktrader.BarServiceBean.addBarToMongoDB(BarServiceBean.java:196) [magicktrader.jar:0.6.0-SNAPSHOT]
        at com.magick.magicktrader.BarServiceBean.getBar(BarServiceBean.java:40) [magicktrader.jar:0.6.0-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_10]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_10]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_10]
        at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_10]

方法调用

    MongoURI uri = null;
        uri = new MongoURI("mongodb://localhost:27017");

        try{
    mongo = new Mongo(uri);
    } catch (NumberFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}       
    //prop.getProperty("database")
    db = mongo.getDB("charts");
    db.requestStart();
    //prop.getProperty("collection")
    dbCollection = db.getCollection("bars");


    /**
     * Bar Structure - attributes
     * 
     * symbol,low,high
     * open,close,time
     * volume;
     **/
    for (Bar bar : barList) {
        document = new BasicDBObject();
        document.put("symbol", bar.getSymbol());
        document.put("low", bar.getLow());
        document.put("high", bar.getHigh());
        document.put("open", bar.getOpen());
        document.put("close", bar.getClose());
        document.put("time", bar.getTime());
        document.put("volume", bar.getVolume());
        //dbCollection.insert(document);
        //prop.getProperty("collection").trim()
        dbCollection.getCollection("bars");
        dbCollection.save(document, WriteConcern.NORMAL);
    }

3 个答案:

答案 0 :(得分:1)

1>这可能是连接不同的问题,我遇到了同样的问题,因为“我多次调用相同的connectionClass”。我通过使连接类单例修复它。我使用MongoClient类进行连接。所以我将用文字解释它,第一次获得mongoDb的连接,从第二次使用相同的连接。 我也是蒙古人的新人,希望这是有效的。

答案 1 :(得分:0)

1最好检查的是

  • 检查mongodb是否正在运行
  • 检查用户名和密码是否正确
  • 检查您尝试用于访问数据库的用户名是否具有正确的所有者或

    必须像这样添加

    db.addUser("test","test") ------- this will add the default username and password in which the app uses.
    check if the user is created properly db.system.users.find()   ----- you should see you new user in the returned list
    log out .... db.runCommand({"logout":1})
    use databasename
    db.auth("test","test") //do the authorizxation
    

答案 2 :(得分:0)

我有同样的问题,我解决了编辑配置文件:/etc/mongod.conf并执行此更改:

#port = 27017 -> port = 27017
bind_ip = 127.0.0.1 -> #bind_ip = 127.0.0.1

这样,所有机器都可以连接到你的mongoDB。

记得重启mongoDB(" sudo service mongod restart")

在此之后,我更新了最新版本的驱动程序(2.12.0),那就可以了。