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

September 13, 2017, at 10:10 PM
MongoClient m = new MongoClient(new ServerAddress("",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. 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


com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector

Answer 1

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. More explanation can be seen on MongoDB site here.

The serverSelectionTimeoutMS variable gives the amount of time in milliseconds that drivers should allow for server selection before giving up and raising an error. Users can set this higher or lower depending on whether they prefer to be patient or to return an error to users quickly (e.g. a "fail whale" web page). The default is 30 seconds, which is enough time for a typical new-primary election to occur during failover.

Converting XML+XSL in HTML using Java

Converting XML+XSL in HTML using Java

We have data as XML and there are multiple formatting XSL stylesIt was working fine till now in IE

How can I parse a csv in low memory, using some parser in Java?

How can I parse a csv in low memory, using some parser in Java?

I used InputStream, and on parsing, if there is a "," in one column then it considers it as a separate columnex - abc, xyz, "m,n" then the parsed output is abc , xyz, m, n Here m and n are considered as separate columns

Spring JdbcTemplate &ldquo;insert into.. select from..&rdquo; not working

Spring JdbcTemplate “insert into.. select from..” not working

I am trying to execute following SQL with Spring JdbcTemplate:

@RolesAllowed has no effect on my RESTful Service

@RolesAllowed has no effect on my RESTful Service

I'm having some trouble with setting up my secure REST-ServiceI wanted to create a simple login/logout service and mess around with it