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