[英]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
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.