MongoDB-Java驱动程序:插入失败时捕获异常

时间:2011-08-03 12:20:16

标签: java mongodb

我正在做一个非常基本的插件:

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
}

假设_databaseName不正确,让我们说驱动程序无法连接到数据库。插入操作很明显,但有三件事:

  • 它永远不会抛出MongoException
  • 我可以在'catch'块中捕获的唯一例外是泛型'java null pointer exception'
  • mongoDb和集合对象已创建且不为null

但是,在我的Eclipse控制台中,我可以清楚地看到更详细的异常消息,如:

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

有没有办法捕获此异常?

由于

修改

遗憾的是,NullPointerException不包含堆栈跟踪,只是一个微不足道的“java.lang.NullPointerException”。但是,这是我在控制台中看到的,在抛出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)

这就是我想要抓住的东西,但遗憾的是似乎无法做到这一点......

2 个答案:

答案 0 :(得分:2)

我能够重现行为,实际上,当您尝试将对象插入无法访问的MongoDB实例时,您将只能捕获NullpointerException。恕我直言,这种行为应该在MongoDB Java驱动程序中修复,因为它不是非常Java。脏的解决方法看起来可能是这样的:

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);
    }
}

答案 1 :(得分:0)

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

在try块中。