Mathieu
Mathieu

Reputation: 1677

MongoDB-Java driver: Catch exception when insert fails

I'm doing a really basic insert like this:

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
}

Let's say for whatever reason that the _databaseName is incorrect, so the driver can't connect to the database. The insert operation fails, obviously but there's 3 things:

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

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

Is there a way to catch this exception?

Thanks

Edit

The NullPointerException contains no stacktrace unfortunately, only a meagre "java.lang.NullPointerException". However, here is what I see in the console, before the NullPointerException is thrown:

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)

That's what I want to catch, but there seems to be no way to do so unfortunately...

Upvotes: 4

Views: 9335

Answers (2)

Matt
Matt

Reputation: 17629

I was able to reproduce the behaviour and in fact you will only be able to catch a NullpointerException when you try to insert an object into an unreachable MongoDB instance. IMHO this behaviour should be fixed in the MongoDB Java Driver, as it is not very Java-ish. The dirty workaround looks probably 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);
    }
}

Upvotes: 2

MeBigFatGuy
MeBigFatGuy

Reputation: 28568

put the

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

in the try block too.

Upvotes: 0

Related Questions