2

我正在尝试从部署在 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);
    }
4

3 回答 3

1

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

于 2013-11-26T11:25:20.607 回答
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
    
于 2014-02-27T08:39:35.290 回答
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),这就是工作。

于 2014-04-22T10:10:44.597 回答