Recently we have upgraded our Sitecore to version 9.0.1 XP0 version on my local machine and used same databases & XConnect in to different environments. Understanding and setting up XConnect in local environment was a bit of hardship, particularly with the SSL certificates. With various blog posts online I could finally make my local environment working.
We had some series of issues with migration of XConnect on various environments but what annoyed the most was XdbContext Batch execution exception in XConnect logs.
Stack trace in logs showed us
Sitecore.Xdb.Collection.Failures.DataProviderException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> Sitecore.Xdb.Collection.Failures.DataProviderException.
As every other developer my natural inclination was that any one of the databases in connection strings was in accessible or the user did not have necessary permissions. Even after checking database servers and permissions XConnect was still giving same exception.
After lot of deliberation we found that _ShardManagement.ShardsGlobal table in Xdb.Collection.ShardMapManager database contains server name which is used by XConnect to communicate with various services.
Since I had copied databases from my local environment to other environments, ShardMapManager had my local db name in it. UAT environment XConnect was trying to connect to my local DB and was giving XdbContextLogging exception.
Replaced server name with respective environments and everything started working fine :)