简体   繁体   English

(MongoDB Java驱动程序(3.0.0-rc0))UnknownHostException&Authentication

[英](MongoDB Java Driver (3.0.0-rc0)) UnknownHostException & Authentication

Recently I started using the new MongoDB Java Driver (3.0.0-rc0). 最近,我开始使用新的MongoDB Java驱动程序(3.0.0-rc0)。 At the moment I have two problems. 目前,我有两个问题。

  • The ServerAddress class isn't throwing the UnknownHostException anymore, I used this exception to determine if a connection succeed or not, what should I use for this now? ServerAddress类不再抛出UnknownHostException ,我使用此异常来确定连接是否成功,现在该如何使用?

  • The DB class now became the MongoDatabase class. DB类现在变成了MongoDatabase类。 When the DB class wasn't deprecated I used the authenticate(String username, char[] password) to authenticate, but now I need to use the MongoCredential class, how do I check if the authentication succeed or not? 当不赞成使用DB类时,我使用了authenticate(String username,char [] password)进行身份验证,但是现在我需要使用MongoCredential类,如何检查身份验证是否成功?

With regards, Julian vd Berkmortel 致谢,朱利安(Julian)vd Berkmortel

The 3.0 Java driver takes the position that 3.0 Java驱动程序的位置是

  • host names that aren't in DNS DNS中没有的主机名
  • unreachable hosts 无法访问的主机
  • incorrect authentication credentials 不正确的身份验证凭据

are severe application configuration errors that generally require human intervention and are therefore only surfaced in the API indirectly through exceptions thrown in the course of normal use of the driver. 是严重的应用程序配置错误,通常需要人工干预,因此仅在驱动程序正常使用过程中通过抛出异常而间接地在API中浮出水面。

So in both these cases the driver will eventually throw a MongoTimeoutException from any method that needs to connect to a MongoDB server. 因此,在这两种情况下,驱动程序最终都会从需要连接到MongoDB服务器的任何方法中引发MongoTimeoutException。 The exception will include a message indicating the root cause of the connection failure. 该异常将包括一条消息,指示连接失败的根本原因。 For instance, the following program: 例如,以下程序:

    MongoClient mongoClient = new MongoClient(new ServerAddress("unknown-host-name"),
                                              MongoClientOptions.builder()
                                                                .serverSelectionTimeout(5000)
                                                                .build());
    mongoClient.getDatabase("admin").runCommand(new Document("ping", 1));

will throw an exception with this message: 将在此消息中引发异常:

   Timed out after 30000 ms while waiting for a server that matches
   ReadPreferenceServerSelector{readPreference=primary}. 
   Client view of cluster state is {type=UNKNOWN, 
   servers=[{address=unknown-host-name:27017, type=UNKNOWN, state=CONNECTING, 
   exception={com.mongodb.MongoSocketException: unknown-host-name}, 
   caused by {java.net.UnknownHostException: unknown-host-name}}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM