Java MongoClient连接成功与否

时间:2018-03-23 13:47:01

标签: mongodb mongodb-java

我正在使用"新的和更好的"如下所示创建MongoClient的方法

String MONGO_URI = "mongodb://username:password@127.0.0.1:27017";
MongoClientURI mongoClientURI = new MongoClientURI(MONGODB_URI);
mongoClient = new MongoClient(mongoClientURI);

问题是:如何检查mongoClient是否成功创建?

MongoClient 构造函数可能会失败,原因是字符串中的任何内容都不正确(地址错误,凭据不正确等)。想要在错误发生时立即发现错误。

没有异常捕获,也没有看到mongoClient中的任何函数来检查它。

尝试以下

try {
    mongoClient = new MongoClient(mongoClientURI);
    MongoDatabase admin_db = mongoClient.getDatabase(WELL-KNOWN-DB-NAME);
} catch (Exception e) {
    e.printStackTrace();
}

两个函数调用没有捕获任何内容。

连接设置失败,由Mongodb库在控制台上打印的异常信息确认:

INFO: Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='admin', source='admin', password=<hidden>, mechanismProperties={}}
    at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:61)
    at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32)
    at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
    at java.lang.Thread.run(Thread.java:748)
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }
    at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170)
    at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123)
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
    at com.mongodb.connection.SaslAuthenticator.sendSaslContinue(SaslAuthenticator.java:99)
    at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:58)
    ... 6 more

1 个答案:

答案 0 :(得分:0)

这:new MongoClient()如果无法与服务器通信,将抛出异常。

例如:

com.mongodb.MongoTimeoutException: Timed out after 1 ms while waiting for a server 

更普遍;它将抛出MongoClientException的子类,它是RuntimeException的子类,因此不会被MongoClient构造函数抛出。因此,如果您想要捕获在创建MongoClient时抛出的任何异常......

try {
    String MONGO_URI = "mongodb://username:password@127.0.0.1:27017";
    MongoClientURI mongoClientURI = new MongoClientURI(MONGODB_URI);
    mongoClient = new MongoClient(mongoClientURI);
} catch (Exception ex) {
    // ... do something
}

此外,如果其基础服务器不再可用,则任何后续尝试使用MongoClient实例都将失败。如果要实现定期运行状况检查以验证底层服务器是否仍然可用,那么“获取”一个众所周知的数据库可能是一种简单,可靠的测试成功/引发故障的方法。例如:

MongoDatabase db = mongoClient.getDatabase("TheNameOfADatabaseToBeUsedForHealthChecking");
// you have to force the execution of a server side action to prove that the undelrying server is available
db.getCollection("TheNameOfASmallCollection").count();