title: MongoDB - find() calls getting stuck sometimes - Timeout
body: <p>We are using <code>MongoDb</code> for saving and fetching data.</p>
  
  <p>All calls that are putting data into collections are working fine and are through common method.</p>
  
  <p>All calls that are fetching data from collections are working fine sometimes and are through common method.</p>
  
  <p>But Sometimes, <strong>only for one of the collection</strong>, <strong>i get my calls being stuck for forever, consuming CPU usage</strong>. I have to manually kill the threads otherwise it consumes my whole CPU.</p>
  
  <p><strong>Mongo Connection</strong></p>
  
  <pre><code>MongoClient mongo = new MongoClient(hostName , Integer.valueOf(port));
  DB mongoDb = mongo.getDB(dbName);
  </code></pre>
  
  <p><strong>Code To fetch</strong></p>
  
  <pre><code>DBCollection collection = mongoDb.getCollection(collectionName);
  DBObject dbObject = new BasicDBObject("_id" , key);
  DBCursor cursor = collection.find(dbObject);
  </code></pre>
  
  <p>Though i have figured out the collection for which it is causing issues, but how can i improve upon this, since it is occurring for this particular collection and sometimes.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>Code to save</strong></p>
  
  <pre><code>DBCollection collection = mongoDb.getCollection(collectionName);
  DBObject query = new BasicDBObject("_id" , key);
  DBObject update = new BasicDBObject();
  update.put("$set" , JSON.parse(value));
  collection.update(query , update , true , false);
  </code></pre>
  
  <p><strong>Bulk Write / collection</strong></p>
  
  <pre><code>DB mongoDb = controllerFactory.getMongoDB();
          DBCollection collection = mongoDb.getCollection(collectionName);
  
          BulkWriteOperation bulkWriteOperation = collection.initializeUnorderedBulkOperation();
  
          Map&lt;String, Object&gt; dataMap = (Map&lt;String, Object&gt;) JSON.parse(value);
  
          for (Entry&lt;String, Object&gt; entrySet : dataMap.entrySet()) {
              BulkWriteRequestBuilder bulkWriteRequestBuilder = bulkWriteOperation.find(new BasicDBObject("_id" ,
                      entrySet.getKey()));
  
              DBObject update = new BasicDBObject();
              update.put("$set" , entrySet.getValue());
  
              bulkWriteRequestBuilder.upsert().update(update);
          }
  </code></pre>
  
  <p>How can i set timeout for fetch calls..??</p>