Azure can close MongoDB connections


Description

Sitecore XP 7.5 and later versions use MongoDB databases to collect and process analytics data. These Sitecore XP versions can be hosted in Azure. Azure can close idle MongoDB connections, so analytics can fail and the following errors appear in log files:

ERROR MongoDbDictionary.Store() has failed.
Exception: Sitecore.Analytics.DataAccess.DatabaseNotAvailableException
Message: Database not available
Source: Sitecore.Analytics.MongoDB
   at Sitecore.Analytics.Data.DataAccess.MongoDb.MongoDbCollection.Execute(Action action, ExceptionBehavior exceptionBehavior)
   at Sitecore.Analytics.Data.DataAccess.MongoDb.MongoDbCollection.Save[TNominalType](TNominalType value)
   at Sitecore.Analytics.Data.DataAccess.MongoDb.MongoDbDictionary.Store(Object value)
ERROR Exception when executing agent aggregation/automationRangeManager
Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.
Source: Sitecore.Analytics.MongoDB
   at Sitecore.Analytics.RangeScheduler.MongoDbRangeMap2.GetRightKey()    at Sitecore.Analytics.Automation.Aggregation.Data.Processing.RangeManagerAgent.Execute()    at Sitecore.Analytics.Core.BackgroundService.Run()

Solution

To resolve this issue, set the maxIdleTimeMS parameter to 60000 in all MongoDB connection strings. You can read more about the format of MongoDB connection strings and supported parameters on the MongoDB documentation site.

<add name="analytics" connectionString="mongodb://user:password@host/analytics?replicaSet=rs1;ssl=true;maxIdleTimeMS=60000" />
<add name="tracking.live" connectionString="mongodb://user:password@host/tracking_live?replicaSet=rs1;ssl=true;maxIdleTimeMS=60000" />
<add name="tracking.history" connectionString="mongodb://user:password@host/tracking_history?replicaSet=rs1;ssl=true;maxIdleTimeMS=60000" />
<add name="tracking.contact" connectionString="mongodb://user:password@host/tracking_contact?replicaSet=rs1;ssl=true;maxIdleTimeMS=60000" />

Sitecore XP will then recycle idle MongoDB connections before Azure tries to close them, and establish new ones when needed.

Note 

You can separate parameters in a connection string using:

If one separator does not work (causes a configuration error), try using another one.