title: What is the solution "com.mongodb.MongoSocketReadException" and "com.mongodb.MongoTimeoutException"
body: <p>I use mongo-java-driver 3.1.0. There is no problem connection with JAVA and MongoDB. I can get data from MongoDB and write data into MongoDB by using JAVA.</p>
  
  <p>But when I try to write more data, I get this fault on my java console. I think when socket connection lasts more than 30 seconds, MongoDB client crushes. Until 30 seconds some part of data is inserted to MongoDB (myCollection)
  First I get;</p>
  
  <blockquote>
    <p>com.mongodb.MongoSocketReadException: Prematurely reached end of
    stream</p>
  </blockquote>
  
  <p>and then</p>
  
  <blockquote>
    <p>com.mongodb.MongoTimeoutException: Timed out after 30000 ms while
    waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}</p>
  </blockquote>
  
  <p>I did not set any timeout so its timeout should be infinity.</p>
  
  <p>Can someone help   </p>
  
  <pre><code>public void writeDB() {
  
      MongoClient mongoClient = new MongoClient ("127.0.0.1", 27017);
      mongoClient.setWriteConcern(WriteConcern.JOURNALED);
  
      DB database = mongoClient.getDB("myCollection");    
      BasicDBObject newDataObject = new BasicDBObject();    
      DBCollection collection = database.getCollection("myCollection");    
  
      int number = 0;    
      for (int i = 0; i &lt;= 1000000; i++) {
      if(CHECKDATA_EXIST) {
          int randomCount = (10 + (Math.random() * 300));    
          for (int j = 0; j &lt; randomCount; j++) {
              number = i + randomCount;
          }
  
          newDataObject.put("_id", i);
          newDataObject.put("myNumber", number);    
          collection.insert(newDataObject);
      }
      }
  }
  </code></pre>
  
  <p>By the way after crush I cannot write or get any data anymore. To write data, I have to restart "mongod".</p>
  
  <p>I added CHECKDATA_EXIST method for if case (my code includes that). I forgot to add that. That method checks id is exist or not.</p>