[JAVA-649] Connection timeout is ignored in ConnectionStatus Created: 21/Sep/12  Updated: 26/Nov/12  Resolved: 16/Nov/12

Status: Closed
Project: Java Driver
Component/s: Configuration
Affects Version/s: 2.9.1
Fix Version/s: 2.10.0

Type: Bug Priority: Major - P3
Reporter: Lukas Krecan Assignee: Jeffrey Yemin
Resolution: Done Votes: 3
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Default connection timeout is used in ConnectionStatus class causing the following code stopping after 20s instead of 1s.

        MongoOptions options = new MongoOptions();
        options.connectTimeout = 1000;
 
        Mongo m = new Mongo(Arrays.asList(new ServerAddress("localhost:27017")), options);
        try {
            m.getDB("test").collectionExists("test");
        } finally {
            m.close();
        }



 Comments   
Comment by auto [ 15/Nov/12 ]

Author:

{u'date': u'2012-11-14T19:25:29Z', u'email': u'jeff.yemin@10gen.com', u'name': u'Jeff Yemin'}

Message: JAVA-649: Using connectTimeout from MongoOptions of the Mongo instance for all the timeouts in ReplicaSetStatus, MongosStatus, and DynamicConnectionStagtus
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/39843408665e033fc7b87bf6491a1d094a8f9620

Comment by Jafar Aliyev [ 05/Nov/12 ]

I can offer the following workaround.

         Mongo m = new Mongo(Arrays.asList(new ServerAddress("localhost:27017")));        
         DB db = mongo.getDB("test"); 
         new Thread(new Runnable() {
            @Override
            public void run() {
                db.authenticate("testUser", "testPwd".toCharArray());
            }
        }).start();
        try {
            Thread.sleep(2000);// wait for 2 sec 
        } catch (InterruptedException ex) {
            // ignore it
        }
        if (!db.isAuthenticated()) {
             mongo.close();
             // you can raise here your own exception
        }

But in this case you have to run MongoDB server with -auth option:

Comment by Jeffrey Yemin [ 21/Sep/12 ]

The way to fix this is for com.mongodb.ConnectionStatus#ensureMaster to take a timeout as an argument.

Comment by Lukas Krecan [ 21/Sep/12 ]

I have forgotten to mention that it happens only if the server is not accessible.

Generated at Thu Feb 08 08:52:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.