简体   繁体   English

使用Java验证mongodb时,它花费了更多时间,并且在凭据错误的情况下抛出mongotimeoutException

[英]While authenticating mongodb using java it is taking more time and throwing mongotimeoutException in case of wrong credential

MongoClient m = new MongoClient(new ServerAddress("182.178.0.29",27017), 
        Arrays.asList(MongoCredential.createCredential("username", "employeedb", "password".toCharArray())));
MongoDatabase md = m.getDatabase("employeedb");
MongoIterable<String> strings = md.listCollectionNames();
MongoCursor<String> iterator = strings.iterator();  

After authentication i need to show message to end user. 身份验证后,我需要向最终用户显示消息。 But, the exception is comming after 30 seconds in case when the user enters wrong credentials.User needs to wait untill the msg dialog comes. 但是,如果用户输入了错误的凭据,则异常会在30秒后出现。用户需要等待直到msg对话框出现。 Could you please check why it is taking that much time and is there any other way to authenticate. 您能否检查一下为什么要花费那么多时间,还有其他任何方法可以进行身份​​验证。

MongoDB version: 3.2.14 java driver version: 3.2.1 MongoDB版本:3.2.14 Java驱动程序版本:3.2.1

Exception: 例外:

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector com.mongodb.MongoTimeoutException:等待30000毫秒后等待与ReadPreferenceServerSelector匹配的服务器

If you know why your authentication is failing ( you mentioned wrong credentials) , then you can customize the timeout period for failing authentication using serverSelectionTimeout property so that you can show your users the failed authentication rather quickly. 如果您知道身份验证失败的原因(您提到了错误的凭据),则可以使用serverSelectionTimeout属性自定义身份验证失败的超时时间,以便可以快速向用户显示失败的身份验证。 More explanation can be seen on MongoDB site here . 更多的解释可以在MongoDB的网站可以看到这里

The serverSelectionTimeoutMS variable gives the amount of time in milliseconds that drivers should allow for server selection before giving up and raising an error. serverSelectionTimeoutMS变量提供了驱动程序在放弃并引发错误之前应该允许服务器选择的时间(以毫秒为单位)。 Users can set this higher or lower depending on whether they prefer to be patient or to return an error to users quickly (eg a "fail whale" web page). 用户可以根据自己的喜好还是快速将错误返回给用户(例如“鲸鱼”网页)来将其设置为较高或较低。 The default is 30 seconds, which is enough time for a typical new-primary election to occur during failover. 默认值为30秒,这足以在故障转移期间进行典型的新主选举。

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

相关问题 com.mongodb.MongoTimeoutException 使用带有列表 ServerAddress 的 MongoClient 时 - com.mongodb.MongoTimeoutException when using MongoClient with list ServerAddress MongoTimeoutException:没有服务器匹配 - MongoDB Atlas - MongoTimeoutException: No Server Match - MongoDB Atlas 使用DI时,播放2.4在第一次请求时花费更多时间 - Play 2.4 taking much more time on first request while using DI com.mongodb.MongoTimeoutException:等待连接时 10000 毫秒后超时 - com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect 为什么使用Java的流API收集器进行管道分组需要更多时间? - Why Pipeline grouping using Java's stream API collectors taking more time? 关于从MongoDB中删除的com.mongodb.MongoTimeoutException - com.mongodb.MongoTimeoutException on deleting from MongoDB Java Spring 启动 Mongodb 花费太多时间来检索结果 - Java Spring boot Mongodb taking too much time to retrieve results Java抛出错误的异常 - Java throwing wrong exception 雪花:虽然使用结果缓存,但相同的查询需要更多时间 - Snowflake: same queries taking more time although using Result Cache 如果 Selenium 花费更多时间以防下拉列表中不存在值,我该怎么办? - What do i do if Selenium taking more time in case value is not present in dropdown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM