2011-08-03 25 views
4

我做這樣一個非常基本的插入:的MongoDB的Java驅動程序:捕捉異常,當插入失敗

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
  • 我可以在我的「抓」塊趕上唯一的例外是一個通用的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) 

這就是我要趕什麼,但似乎沒有辦法做到這一點很遺憾......

+0

你試過顯式地捕獲一個'IOException'嗎? – mre

+0

是的,我嘗試過,但它永遠不會拋出...... – Mathieu

+0

在抓到'Exception'之前'catch''IOException'嗎? – mre

回答

2

我能夠重現這種行爲,實際上當您嘗試將對象插入到無法訪問的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); 
    } 
} 
+0

是的,我想我不會有其他選擇,只能做這樣的事情......謝謝你的回答! – Mathieu

0

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

也在try塊中。

+0

已經建議。看到我上面的評論。 – mre

+0

是的,我嘗試過,但沒有運氣:/ – Mathieu