title: MongoDB Java API com.mongodb.DBPortPool gotError
body: <p>I'm currently working on a project using the MongoDB Java API.  I have been working on this project for a while, but have recently come across an issue that I cannot resolve.  I am trying to make a database system that is fault tolerant.  To simulate a database crashing, I have my program connect to a Mongodb server that I have made, execute a simple read or write, and then shut down the database server.  I had originally thought that this would cause certain methods that I am calling to throw a <code>MongoException</code> that I could catch and then recover from the database crash.  However, I am getting a strange stack trace that says I am throwing an EOFException, among other things.  Below is the stack trace itself.</p>
  
  <pre><code>Mar 04, 2013 8:06:15 PM com.mongodb.DBPortPool gotError                     
  WARNING: emptying DBPortPool to polaris.cs.wcu.edu/152.30.5.5:12345 b/c of error
  java.io.EOFException                                                        
      at org.bson.io.Bits.readFully(Bits.java:48)                             
      at org.bson.io.Bits.readFully(Bits.java:33)                             
      at org.bson.io.Bits.readFully(Bits.java:28)                             
      at com.mongodb.Response.&lt;init&gt;(Response.java:40)                        
      at com.mongodb.DBPort.go(DBPort.java:124)                               
      at com.mongodb.DBPort.call(DBPort.java:74)                              
      at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:282)        
      at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:256)             
      at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:289)      
      at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:274)      
      at com.mongodb.DBCursor._check(DBCursor.java:368)                       
      at com.mongodb.DBCursor._hasNext(DBCursor.java:459)                     
      at com.mongodb.DBCursor.hasNext(DBCursor.java:484)                      
      at edu.wcu.cs.capstone.view.AbstractViewEngine.getView(AbstractViewEngine.java:57)
      at edu.wcu.cs.capstone.transaction.ServerTransactionManager.getView(ServerTransactionManager.java:52)
      at edu.wcu.cs.capstone.transaction.ServerTransactionManager.run(ServerTransactionManager.java:183)
      at java.lang.Thread.run(Thread.java:722)                                
  
  Caught exception                                                            
  Mar 04, 2013 8:06:15 PM com.mongodb.DBPortPool gotError                     
  WARNING: emptying DBPortPool to polaris.cs.wcu.edu/152.30.5.5:12345 b/c of error
  java.io.IOException: couldn't connect to [polaris.cs.wcu.edu/152.30.5.5:12345] bc:java.net.ConnectException: Connec
      at com.mongodb.DBPort._open(DBPort.java:214)                            
      at com.mongodb.DBPort.go(DBPort.java:107)                               
      at com.mongodb.DBPort.call(DBPort.java:74)                              
      at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:282)        
      at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:256)             
      at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:289)      
      at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:274)      
      at com.mongodb.DBCursor._check(DBCursor.java:368)                       
      at com.mongodb.DBCursor._hasNext(DBCursor.java:459)                     
      at com.mongodb.DBCursor.hasNext(DBCursor.java:484)                      
      at edu.wcu.cs.capstone.view.AbstractViewEngine.getView(AbstractViewEngine.java:61)
      at edu.wcu.cs.capstone.transaction.ServerTransactionManager.getView(ServerTransactionManager.java:52)
      at edu.wcu.cs.capstone.transaction.ServerTransactionManager.run(ServerTransactionManager.java:183)
      at java.lang.Thread.run(Thread.java:722)                                
  
  DB is down.                                                                 
  Exception in thread "Thread-3" java.lang.NullPointerException               
      at edu.wcu.cs.capstone.transaction.ServerTransactionManager.run(ServerTransactionManager.java:184)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>The <code>Caught Exception</code> and <code>DB is down.</code> are print statements I am using to verify I am catching certain exceptions.  Here is the relevant code:</p>
  
  <pre><code>public View getView(Mongo mongo, Query query) throws MongoException,        
                                                       EOFException {         
      String connected = "";                                                  
      try {                                                                   
          connected = mongo.getConnectPoint();                                
      } catch (Exception e) {                                                 
          throw new MongoException("Error.");                                 
      }                                                                       
      System.out.println("Connected: " + connected);                          
  
      DB           db         = mongo.getDB(query.getServer());               
      List&lt;DBObject&gt; viewList   = new ArrayList&lt;DBObject&gt;();                  
  
      DBCollection collection = db.getCollection(query.getCollection());      
      DBCursor     cursor     = collection.find(query.getQuery(), excludeID); 
  
      try {                                                                   
          cursor.hasNext();                                                   
      } catch (Exception e) {                                                 
          System.out.println("Caught exception");                             
      }                                                                       
      while (cursor.hasNext()) {                                              
          viewList.add(cursor.next());                                        
      }                                                                       
  
      return new View(viewList);                                              
  }
  </code></pre>
  
  <p>As you can see, the error is occurring when I call <code>cursor.hasNext()</code>.  I am also actually still catching the exception that is being thrown because of the <code>Caught exception</code>.  However, I am still getting a stack trace as if it was not being caught.  I am suspicious that this has something to do with the <code>DBPortPoolgotError()</code> method, but I have looked at the code for this method, and cannot determine what it is actually doing or even how it is being called.  (<a href="http://grepcode.com/file/repo1.maven.org/maven2/org.mongodb/mongo-java-driver/2.1/com/mongodb/DBPortPool.java#DBPortPool.gotError%28java.lang.Exception%29" rel="nofollow">GrepCode link</a>)</p>
  
  <p>As stated above, I thought the behavior for this type of code would have been to throw a <code>MongoException</code> when a call on that specific <code>Mongo</code> object failed because the database was no longer active.  Any help that anyone could provide would be greatly appreciated!</p>