0
votes

Mongodb version - mongodb-win32-i386-2.4.8
mongodb java driver version - mongo-java-driver-2.12.0

Code

    DB db = mongoClient.getDB(getDbName());
    DBCollection collection = db.getCollection(getCollectionName());
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put("name","John Doe");
    collection.save(dbObject);
    mongoClient.close();

Stacktrace

Jan 05, 2014 7:26:50 PM com.mongodb.DBPortPool gotError WARNING: emptying DBPortPool to localhost/127.0.0.1:27017 b/c of error java.lang.IllegalArgumentException: 'ok' should never be null... at com.mongodb.CommandResult.ok(CommandResult.java:43) at com.mongodb.CommandResult.throwOnError(CommandResult.java:109) at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:102) at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:142) at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:115) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:248) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:204) at com.mongodb.DBCollection.insert(DBCollection.java:148) at com.mongodb.DBCollection.insert(DBCollection.java:91) at com.mongodb.DBCollection.save(DBCollection.java:810) at com.mongodb.DBCollection.save(DBCollection.java:786)

Mongod console gives the below trace

Sun Jan 05 19:38:08.289 [initandlisten] connection accepted from 127.0.0.1:50112 #5 (1 connection now open) Sun Jan 05 19:38:08.300 [conn5] assertion 16256 Invalid ns [

                                    tinyblogdb

                    .$cmd] ns:

                                    tinyblogdb

                    .$cmd query:{ getlasterror: 1 } Sun Jan 05 19:38:08.300 [conn5]       ntoskip:0 ntoreturn:-1 Sun Jan 05 19:38:08.300

[conn5] end connection 127.0.0.1:50112 (0 connections no w open)

Any idea why this happens ?

UPDATE

If I hardcode the db name to the variable 'dbn' , the data gets inserted successfully , what I tried before was setter injection with spring in the method getDbName() , the below code works ! Now I am super curious why it doesn't work , when I inject the dbName ?

    String dbn = "tinyblogdb";
    MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
    DB db = mongoClient.getDB(dbn);
    DBCollection collection = db.getCollection(getCollectionName());
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put("name","John Doe");
    collection.save(dbObject);
2

2 Answers

0
votes

Found out the reason why it wasn't working while doing setter injection , here is the snippet of the spring injection.Even though I put the value in CDATA section , the white spaces were present and the space were also read as the part of the string.Trimming the string solved it.silly me.

    <![CDATA[ 

    tinyblogdb

                ]]>

    </value>
</property>
0
votes

Confirm that getDbName() is returning something valid. If it's an injected value you're almost certainly not getting injected properly and are passing in null.