In my service, I have a background thread that best saves the thread stream of an object of a certain type. The code looks something like this:
while (AllowRun)
{
try
{
using (DbContext context = GetNewDbContext())
{
while (AllowRun && context.GetConnection().State == ConnectionState.Open)
{
TEntity entity = null;
try
{
while (pendingLogs.Count > 0)
{
lock (pendingLogs)
{
entity = null;
if (pendingLogs.Count > 0)
{
entity = pendingLogs[0];
pendingLogs.RemoveAt(0);
}
}
if (entity != null)
{
context.Entities.Add(entity);
}
}
context.SaveChanges();
}
catch (Exception e)
{
}
}
}
}
catch (Exception e)
{
}
}
(this is basically the actual code, I skipped a few irrelevant parts that try to store pop-up objects in memory until we can save the material in the database again when the exception hits the block (1))
, , , Db. , - , . , ( , ), context.SaveChanges() ( (1)):
System.Data.EntityException: An error occurred while starting a transaction on the provider connection. See the inner exception for details. --->
System.InvalidOperationException: The requested operation cannot be completed because the connection has been broken.
, context.GetConnection().State == ConnectionState.Open, true. , , , , . ( AllowRun ). , , , ?
, , "" ? , EntityException , reset , InnerException InvalidOperationException , , reset . , , ConnectionState , , DB. , , ?