2013-03-05 55 views
3

我目前正在使用MongoDB Java API開發一個項目。我一直在做這個項目,但最近遇到了一個我無法解決的問題。我正試圖建立一個容錯的數據庫系統。爲了模擬數據庫崩潰,我將程序連接到我所做的Mongodb服務器,執行簡單的讀取或寫入操作,然後關閉數據庫服務器。我原本以爲這會導致某些方法,我打電話來拋出一個MongoException我可以捕捉然後從數據庫崩潰中恢復。然而,我得到一個奇怪的堆棧跟蹤,說我拋出一個EOFException,等等。以下是堆棧跟蹤本身。MongoDB Java API com.mongodb.DBPortPool gotError

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.<init>(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) 

Caught ExceptionDB is down.是我使用來驗證我趕上某些例外打印報表。下面是相關代碼:

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<DBObject> viewList = new ArrayList<DBObject>();     

    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);            
} 

正如你可以看到,當我打電話cursor.hasNext()錯誤發生。我實際上還在捕捉由於Caught exception而引發的異常。但是,我仍然得到一個堆棧跟蹤,就好像它沒有被捕獲一樣。我懷疑這與DBPortPoolgotError()方法有關,但我已經查看了此方法的代碼,並且無法確定它實際正在執行的操作,甚至無法確定它如何被調用。 (GrepCode link

如上所述,我認爲當這個特定的Mongo對象上的調用因數據庫不再有效而失敗時,這種類型的代碼的行爲將會拋出MongoException。任何人都可以提供的幫助將不勝感激!

+0

您連接的端口是否正確?這是否適用於其他命令?有沒有辦法讓服務器日誌尾巴可以確認mongo正在接收並正確迴應查詢? – ranman 2013-03-07 19:40:41

回答

0

我有同樣的問題。這是因爲我沒有重新啓動我的java服務器(在我的情況下是tomcat)重新啓動了mongod。重新啓動tomcat解決了這個問題,因爲mongo驅動程序丟失了