2015-03-02 256 views
0

請幫幫我。 我在長時間使用mongo數據庫進行應用。應用程序處於開發模式。有時我看到這個錯誤Mongo db超時異常說連接被拒絕

ERROR- Cannot invoke the action, eventually got an error: org.springframework.dao.DataAccessResourceFailureException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]

但是,當我重新啓動服務器,它不會再來。但一旦我不得不重新啓動服務器。我正在使用mongodb 2.6.6。

請幫幫我。我對未來感到恐懼,因爲它很快就會進入現場模式。 我的DatabaseConnection類:

static final String mongoServer = Play.application().configuration().getString("application.mongo.db.server"); 
static final String mongoDBname = Play.application().configuration().getString("application.mongo.db.dbname"); 
static final String mongoUsername = Play.application().configuration().getString("application.mongo.db.username"); 
static final String mongoPassword = Play.application().configuration().getString("application.mongo.db.password"); 
static final int connectionPerHost = Play.application().configuration().getInt("application.mongo.db.connections"); 
static final int connectionIdleTime = Play.application().configuration().getInt("application.mongo.db.idletime"); 

private MongoTemplate _mongoTemplate; 
private static MongoClient _mongo; 

public MongoTemplate getContext() { 
    if(_mongoTemplate == null) 
     openDbConnection(); 
    if(_mongo == null || _mongoTemplate == null) 
     Logger.error("DatabaseConnection::openDbConnection - Unable to get context. How is this possible?"); 
    return _mongoTemplate; 
} 

private static synchronized void createMongo() { 
    if(_mongo == null) { 
     Logger.debug("DatabaseConnection::openDbConnection - Opening a new connection"); 
     MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(connectionPerHost) 
       .cursorFinalizerEnabled(true).maxConnectionIdleTime(connectionIdleTime).build(); 
     MongoCredential credential = MongoCredential.createMongoCRCredential(mongoUsername, mongoDBname, mongoPassword.toCharArray()); 
     ServerAddress addr = null; 
     try { 
      addr = new ServerAddress(mongoServer); 
     } catch (UnknownHostException e) { 
      Logger.error("Error Connecting to Mongo: Wrong Server??", e); 
      e.printStackTrace(); 
     } 
     _mongo = new MongoClient(addr, Arrays.asList(credential), options); 
    } 
} 

private boolean openDbConnection() { 
    try { 
     if(_mongo == null) createMongo(); 
     // TODO: Connection Pooling 
     _mongoTemplate = new MongoTemplate(_mongo, mongoDBname); //new MongoTemplate(dbFactory, converter); 
     return true; 
    } catch (Exception e) { 
     Logger.error("Error Opening Connection:", e); 
     e.printStackTrace(); 
    } 
    return false; 
} 

private boolean closeDbConnection() { 
    try { 
     _mongoTemplate = null; 
     return true; 
    } catch (Exception ex) { 
     Logger.error("Error Closing", ex); 
    } 
    return false; 
} 

@Override 
protected void finalize() throws Throwable { 
    closeDbConnection(); 
    super.finalize(); 
} 
+0

賠率是答案是在您的服務器日誌,所以看看那裏。在這裏沒有看到任何代碼的教育猜測是,你可能已經耗盡了可用的連接。代碼中的某些內容可能是由於創建了比您需要的連接數量更多的連接,或者在沒有其他池管理的情況下產生沉重的負載。查看服務器日誌和代碼以獲取相關的連接信息。 StackOverflow是一個「編碼」問答網站。所以一般我們需要代碼來診斷問題。 – 2015-03-02 08:41:04

+0

@neil lunn.i發佈了我的代碼,thnx的建議 – 2015-03-02 11:08:20

+0

但你有沒有得到重點?關鍵是你可能會創建比你需要的更多的連接。這段代碼似乎沒有必要。潛水員應該處理一個「連接池」,你的注入配置應該使連接可用。看起來這裏沒有提到的「你的代碼的其餘部分」實際上使得「連接」多次並且從不釋放它們。如果你這樣編碼這部分,看起來你不需要的時候多次調用「連接」。這是我的觀點。您需要進一步觀察並刪除多餘的呼叫。 – 2015-03-02 11:16:08

回答

0

通常它出現時沒有建立monogdb連接,驗證它。