MongoDB-Java Driver: Throw Exception on Insert Insert

I am doing a really basic insert:

try { DB mongoDb = _mongo.getDB(_databaseName); DBCollection collection = mongoDb.getCollection(_collectionName); collection.insert(myBasicDBObject); } catch (IOException ex) { // Unreachable code } catch (MongoException ex) { // Exception never thrown } catch (Exception ex) { // Handle exception } 

Say for some reason that _databaseName is incorrect, so the driver cannot connect to the database. The paste operation does not work, obviously, but there are 3 things:

  • He never throws a MongoException
  • The only exception that I can catch in my catch block is the java null pointer exception
  • mongoDb and collection objects are created, not null

However, in my Eclipse console, I can see more detailed exception messages like:

 java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect 

Is there any way to catch this exception?

thanks

Edit

In NullPointerException, unfortunately, there is no stacktrace, but only "java.lang.NullPointerException". However, here is what I see in the console before throwing a NullPointerException:

 2011-08-05 10:06:52 com.mongodb.DBTCPConnector fetchMaxBsonObjectSize ATTENTION: Exception determining maxBSON size using0 java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect at com.mongodb.DBPort._open(DBPort.java:206) at com.mongodb.DBPort.go(DBPort.java:94) at com.mongodb.DBPort.go(DBPort.java:75) at com.mongodb.DBPort.findOne(DBPort.java:129) at com.mongodb.DBPort.runCommand(DBPort.java:138) at com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize(DBTCPConnector.java:419) at com.mongodb.Mongo.getMaxBsonObjectSize(Mongo.java:541) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:237) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:210) at com.mongodb.DBCollection.insert(DBCollection.java:80) at foo.App.main(App.java:25) 2011-08-05 10:06:53 com.mongodb.DBTCPConnector fetchMaxBsonObjectSize ATTENTION: Exception determining maxBSON size using0 java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect at com.mongodb.DBPort._open(DBPort.java:206) at com.mongodb.DBPort.go(DBPort.java:94) at com.mongodb.DBPort.go(DBPort.java:75) at com.mongodb.DBPort.findOne(DBPort.java:129) at com.mongodb.DBPort.runCommand(DBPort.java:138) at com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize(DBTCPConnector.java:419) at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:406) at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:144) at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:137) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:255) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:210) at com.mongodb.DBCollection.insert(DBCollection.java:80) at foo.App.main(App.java:25) 2011-08-05 10:06:54 com.mongodb.DBPortPool gotError ATTENTION: emptying DBPortPool to 127.0.0.1:27017 b/c of error java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect at com.mongodb.DBPort._open(DBPort.java:206) at com.mongodb.DBPort.go(DBPort.java:94) at com.mongodb.DBPort.go(DBPort.java:75) at com.mongodb.DBPort.say(DBPort.java:70) at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:151) at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:137) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:255) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:210) at com.mongodb.DBCollection.insert(DBCollection.java:80) at foo.App.main(App.java:25) 

This is what I want to catch, but there seems to be no way to do this, unfortunately ...

+4
source share
2 answers

I was able to reproduce the behavior, and in fact you will be able to catch a NullpointerException when trying to insert an object into an unreachable instance of MongoDB. IMHO this behavior should be fixed in the MongoDB Java driver, since it is not very Java-ish. A dirty workaround looks something like this:

 private static void safeInsert(DBCollection c, DBObject o) { if (c == null) { throw new RuntimeException("collection must not be null"); } if (o == null) { throw new RuntimeException("object must not be null"); } try { c.insert(o); } catch (NullPointerException e) { throw new RuntimeException("unable to connect to MongoDB " + c.getFullName(), e); } } 
+2
source

put

 DB mongoDb = _mongo.getDB(_databaseName); DBCollection collection = mongoDb.getCollection(_collectionName); 

in the try block.

0
source

All Articles