如何在Java中连接到MongoDB时捕获异常?

时间:2017-05-28 23:05:49

标签: java mongodb exception-handling

如何捕获以下代码导致的异常?

try {
    MongoCredential credential = MongoCredential.createCredential(
                                  cu.getName(), 
                                  "admin",
                                  cu.getPassword().toCharArray());

    ServerAddress address = new ServerAddress("localhost", 27017);

    mongoClient = new MongoClient(address, Arrays.asList(credential));

} catch (MongoSecurityException e) {
    System.out.println("test");
}

我确实在stacktrace中得到了异常,但我无法捕捉它们。

  

Mai 29,2017 1:04:37 AM com.mongodb.diagnostics.logging.JULLogger log   信息:使用设置创建的集群{hosts = [localhost:27017],mode = SINGLE,requiredClusterType = UNKNOWN,serverSelectionTimeout ='30000 ms',maxWaitQueueSize = 500}   Mai 29,2017 1:04:37 AM com.mongodb.diagnostics.logging.JULLogger log   信息:连接到服务器localhost时监视器线程中的异常:27017   com.mongodb.MongoSecurityException:异常验证MongoCredential {mechanism = null,userName ='root',source ='admin',password =,mechanismProperties = {}}       在com.mongodb.connection.SaslAuthenticator.wrapInMongoSecurityException(SaslAuthenticator.java:157)       在com.mongodb.connection.SaslAuthenticator.access 200美元(SaslAuthenticator.java:37)       在com.mongodb.connection.SaslAuthenticator $ 1.run(SaslAuthenticator.java:66)       在com.mongodb.connection.SaslAuthenticator $ 1.run(SaslAuthenticator.java:44)       在com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:162)       在com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:44)       在com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32)       at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:109)       at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:46)       在com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116)       在com.mongodb.connection.DefaultServerMonitor $ ServerMonitorRunnable.run(DefaultServerMonitor.java:113)       在java.lang.Thread.run(未知来源)   引起:com.mongodb.MongoCommandException:命令失败,错误18:'身份验证失败。在服务器localhost:27017。完整响应是{“ok”:0.0,“errmsg”:“身份验证失败。”,“代码”:18,“codeName”:“AuthenticationFailed”}       在com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170)       在com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123)       在com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)       在com.mongodb.connection.SaslAuthenticator.sendSaslContinue(SaslAuthenticator.java:121)       在com.mongodb.connection.SaslAuthenticator.access 100美元(SaslAuthenticator.java:37)       在com.mongodb.connection.SaslAuthenticator $ 1.run(SaslAuthenticator.java:63)       ......还有9个   `

1 个答案:

答案 0 :(得分:0)

您似乎遇到了身份验证问题:

  

com.mongodb.MongoSecurityException:异常验证MongoCredential {mechanism = null,userName ='root',source ='admin',password =,mechanismProperties = {}} at ...`

查看您的代码,我看到它被称为public static void ConvertFlatArrayToMatrix(int[] array, int[,] matrix, int dimension) { for(int i = 0; i < array.Length; i++) { int r = Mathf.FloorToInt(i / dimension); int c = i % dimension; matrix[c,r] = array[i]; } } ,它接受​​3个参数(用户,数据库和密码)。

假设您的凭据正确无误,我强烈怀疑MongoCredential.createCredential()不是您的数据库名称。