title: Mongodb connection fails when selecting or inserting with java
body: <p>I'm using <code>mongo-java-driver-2.11.3.jar</code> and also tried with <code>mongo-2.10.1.jar</code> both are giving the following error.The database is
  connected but the collection insert and selection are not working.</p>
  
  <pre><code>import java.net.UnknownHostException;
  import java.util.Set;
  
  import com.mongodb.BasicDBObject;
  import com.mongodb.DB;
  import com.mongodb.DBCollection;
  import com.mongodb.MongoClient;
  import com.mongodb.MongoException;
  
  public class mongo_test {
      public MongoClient mongo = null;
      public DB mongodb = null;
  
      private void mongo_startconnection() {
          try {
              mongo = new MongoClient("10.0.2.15", 27017);
              mongodb = mongo.getDB("foobar");
              System.out.println("Mongodb is connected");
              BasicDBObject doc = new BasicDBObject();
              DBCollection collection = mongodb.getCollection("url_ta");
              Set&lt;String&gt; colls = mongodb.getCollectionNames();
              for (String s : colls) {
                  System.out.println(s);
              }
              System.out.println("done");
          } catch (UnknownHostException e) {
              e.printStackTrace();
          } catch (MongoException e1) {
              e1.printStackTrace();
          }
      }
  
      public static void main(String args[]) {
          mongo_test MT = new mongo_test();
          MT.mongo_startconnection();
      }
  }
  </code></pre>
  
  <p>I'm getting this error:</p>
  
  <pre><code>Mongodb is connected
  Nov 26, 2013 11:34:39 AM com.mongodb.DBTCPConnector initDirectConnection
  WARNING: Exception executing isMaster command on /10.0.2.15:27017
  java.net.SocketTimeoutException: connect timed out
      at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at com.mongodb.DBPort._open(DBPort.java:223)
      at com.mongodb.DBPort.go(DBPort.java:125)
      at com.mongodb.DBPort.go(DBPort.java:106)
      at com.mongodb.DBPort.findOne(DBPort.java:162)
      at com.mongodb.DBPort.runCommand(DBPort.java:170)
      at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:547)
      at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:526)
      at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:236)
      at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
      at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
      at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
      at com.mongodb.DB.getCollectionNames(DB.java:400)
      at mongo_test.mongo_startconnection(mongo_test.java:29)
      at mongo_test.main(mongo_test.java:69)
  com.mongodb.MongoException$Network: Read operation to server /10.0.2.15:27017 failed on database foobar at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:253)
      at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
      at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
      at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
      at com.mongodb.DB.getCollectionNames(DB.java:400)
      at mongo_test.mongo_startconnection(mongo_test.java:29)
      at mongo_test.main(mongo_test.java:69)
  Caused by: java.net.SocketTimeoutException: connect timed out
      at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at com.mongodb.DBPort._open(DBPort.java:223)
      at com.mongodb.DBPort.go(DBPort.java:125)
      at com.mongodb.DBPort.call(DBPort.java:92)
      at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
      ... 6 more 
  </code></pre>