mongodb java异常_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

>我可以在’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)

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

最佳答案 我能够重现该行为,实际上,当您尝试将对象插入无法访问的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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
最新发布
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值