问题
So, I am using MongoDB
on a Azure VM
and I have a web site hosted on Azure web Sites as a service.
My problem is: Sometimes I get an error like this:
"Exception: MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Sockets.SocketException: 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"
After I got this error, just I access the endpoint again and I have success immediately.
Details:
ConnectionString: mongodb://xxx.aaa.net:1000, xxx.aaa.net:1001, xxx.aaa.net:1002/?readPreference=nearest
Before open the conection with Mongo I set the MaxConnectioIdleTimeout property like this "MongoDefaults.MaxConnectionIdleTime = TimeSpan.FromSeconds(30);" , To keep alive connection, because the idle timeout for azure LoadBalancer become inactivy after 4 minutes by default.
So, I don't know what's going on.
Anybody help me?
回答1:
Yes, 4 minutes. Azure closes a connection session if it keeping on idling up to 4 minutes.But the mongodb driver don't know it,it will still get this connection form connection pool. You can either set the MaxConnectionIdleTime(in mongodb driver settings) less than 4 minutes, or set the max session idle time of azure VM more larger by powershell (30 minutes max).
BTW, if your web site which also hosted on azure access the mongodb server with the private IP(PIP), there won't be this problem.
来源:https://stackoverflow.com/questions/32385566/mongodb-driver-mongoconnectionexception-an-exception-occurred-while-opening-a