2013-08-05 110 views
3

我有一个C#应用程序,通过C#ThreadPool执行多线程插入到MongoDB中。但是,我已经得到了TimeoutException: Timeout waiting for a MongoConnection。我正在使用MongoServer.RequestStart方法,它应该将连接释放回MongoClient连接池。释放连接回到MongoDB连接池

此外,线程池至少有4个线程,最多8个线程,而Mongo连接池默认有100个连接,所以我不应该没有连接。

那么,为什么我会得到这个错误?

下面是传递给线程池的方法。 _client是一个MongoClient实例变量。

public void BatchInsert(string collectionName, BinaryPacketDocument[] documents, int batchSize) { 
     MongoServer server = _client.GetServer(); 
     MongoDatabase database = server.GetDatabase(_databaseName); 
     using (server.RequestStart(database)) { 
      MongoCollection collection = database.GetCollection(collectionName); 
      collection.InsertBatch(documents); 
      StatisticsManager.GetCounter("logs").Add(batchSize);  
     } 
    } 

这就是我如何将它传递给线程池。

private void SendWorkToThreadPool(string collectionName, BinaryPacketDocument[] documents, int batchSize) { 
     if (documents.Length != 0) { 
      ThreadPool.QueueUserWorkItem(state => _inserter.BatchInsert(collectionName, documents, batchSize)); 
     } 
    } 

回答

1

我意识到我的线程池实际上并不限于8个线程。如果将0作为ThreadPool.SetMax/Min线程的参数之一传递,它将无法设置max(但不是显式地)。