【问题标题】:MongoDB-Java driver: Catch exception when insert failsMongoDB-Java 驱动程序:插入失败时捕获异常
【发布时间】:2011-08-05 22:11:52
【问题描述】:

我正在做这样一个非常基本的插入:

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 不正确,因此驱动程序无法连接到数据库。插入操作显然失败了,但有 3 件事:

  • 它从不抛出 MongoException
  • 我可以在“catch”块中捕获的唯一异常是通用“java 空指针异常”
  • mongoDb 和集合对象已创建且不为空

但是,在我的 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)

这就是我想要捕捉的,但不幸的是似乎没有办法这样做......

【问题讨论】:

  • 您是否尝试过明确捕获IOException
  • 是的,我确实尝试过,但它从未被抛出......
  • 在你抓到Exception之前你是catchIOException吗?
  • 事实上,我什至不能使用 catch(IOException ex) 块。 Eclipse 给了我一个 'Unreachable catch block for IOException' 错误。
  • 这是因为IOExceptioncatch 块需要在Exception 块的catch 块之前。如果这不起作用,那么显然该方法不会抛出IOException

标签: java mongodb


【解决方案1】:

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

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】:

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

也在 try 块中。

【讨论】:

  • 是的,我确实尝试过,但没有运气:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多