[TestMethod]
public void TestLoop()
{
var server = MongoServer.Create(
@"mongodb://user:password@dbh74.mongolab.com:2700/XXX");
var database = server["XXX"];
MongoCollection<Item> sourceCollection =database.GetCollection<Item>("Item");
var counter = 0;
int batchSize = 200;
List<item> batch = new List<item>();
foreach (var item in sourceCollection.FindAll().SetBatchSize(batchSize))
{
counter++;
batch.Add(item);
}
}
This is a simple test function to retrieve a collection for testing purpose. It work fine before but it is broken and throw the following error.
Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
This error is throw as the cursor want to get the next batch of data. It seems the connection is dropped so I modified my code for a work around to force reconnect.
[TestMethod]
public void TestLoop()
{
var server = MongoServer.Create(
@"mongodb://user:password@dbh74.mongolab.com:2700/XXX");
var database = server["XXX"];
MongoCollection<Item> sourceCollection =database.GetCollection<Item>("Item");
var counter = 0;
int batchSize = 200;
List<item> batch = new List<item>();
foreach (var item in sourceCollection.FindAll().SetBatchSize(batchSize))
{
//serverX.Reconnect();
counter++;
if (counter% batchSize == 0)
{
server.Reconnect();
}
batch.Add(item);
}
}
I want to know what’s wrong of my orginal code. The only difference thing is my mongodb hosting mongolab just promoted its version to 2.0.2. Any hints is appreciate.
Resolved. It is not a matter of code or db version. There are application running in background and consume the network resource.
After closing that application and re-run the test. The test go fine.