2017-05-28 6 views
-2

次のコードに起因する例外を検出するにはどうすればよいですか?JavaでMongoDBに接続する際に例外をキャッチする方法は?

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で例外を取得しますが、私はそれらを捕まえることはできません。

舞29、2017年1時04分37秒AM com.mongodb.diagnostics.logging.JULLogger ログ情報:設定{ホストは= [ローカルホスト:27017]で作成したクラスタ、UNKNOWNモード= SINGLE、requiredClusterType =情報:サーバーへの接続中にモニタスレッドで例外が発生するlocalhost:27017 com.mongodb .MongoSecurityException:MongoCredentialを認証している例外が例外です。com.mongodc.connection.SaslAuthenticator.wrapInMongoSecurityException(SaslAuthenticator.java:157)で をMongoCredential {mechanism = null、userName = 'root'、source = 'admin'、password =、mechanismProperties = {}}at com.mongodb.connection.SaslAuthenticator.access $ 200(SaslAuthenticator.java:37) at com.mongodb.connection.SaslAuthenticator $ 1.run(SaslAuthenticator.java:66) at com.mongodb.connection.SaslAuthenticator $ 1.run (SaslAuthenticator.java:44) at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:162) at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:44) at com.mongodb.connection com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll com.mongodb.connection.InternalStreamConnectionInitializer.initializeで(InternalStreamConnectionInitializer.java:109) (InternalStreamConnectionInitializer.java:46)で.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116) at com.mongodb.connection.DefaultServerMonitor $ ServerMonitorRunnable.run(DefaultServerMonitor.java:113) at java.lang.Thread.run(不明なソース) 原因:com.mongodb.MongoCommandException:コマンドがエラー18で失敗しました: '認証に失敗しました。'サーバーのlocalhost:27017完全な応答は{"ok":0.0、 "errmsg": "認証に失敗しました"、 "code":18、 "codeName": "AuthenticationFailed"} at 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) at com.mongodb.connection.SaslAuthenticator.access $ 100(SaslAuthenticator.java:37) at com.mongodb.connection.SaslAuthenticator $ 1.run(SaslAuthenticator.java:63) ... 9 more `

+2

残りのバックトレースを見ると、おそらく、スローするコードがtryブロック内にないことがわかります。 – bmargulies

答えて

0

あなたが、認証の問題を抱えているようだ:

com.mongodb.MongoSecurityException:MongoCredential {メカニズム= nullのユーザ名= 'ルート'、ソースは= '管理者' の認証例外、パスワード=、mechanismProperties = {} } at ... `

あなたのコードを見ると、という3つのパラメータ(ユーザー、データベース、パスワード)を受け取ることがわかります。

資格情報が正しいとすれば、私はadminがあなたのデータベース名ではないと強く思っています。

+0

はい、exacltyです。それは私が捕まえたい例外です。 – Max

関連する問題