We have an application that handles XML files. The application uses an EF 6 to save data to the DB. The application handles the files in concurrent threads using ThreadPool.QueueUserWorkItem method. The application has worked fine for 1,5 year. Now we started to get EF exception as following:
Most of the times there are no such error, but it is raised a few times a day. We doest succeeded to figure out what are the reasons or conditions that cause an EF to raise this error.
Any suggestions?
An error occurred while starting a transaction on the provider connection. See the inner exception for details.
at System.Data.Entity.Core.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
at System.Data.Entity.Core.EntityClient.EntityConnection.BeginTransaction()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at Albatross.Core.Utilities.BasicDataRepository`2.Update(T entity) in c:\DevTFS\AlbatrossEmployers\AlbatrossEmployers_DevTiyuvHeshb\Albatross.Core\Utilities\BasicDataRepository.cs:line 177
****** Inner Exception - START ******
SqlConnection does not support parallel transactions.
at System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName, Boolean shouldReconnect)
at System.Data.SqlClient.SqlConnection.BeginTransaction(IsolationLevel iso, String transactionName)
at System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.BeginTransaction(DbConnection connection, BeginTransactionInterceptionContext interceptionContext)
at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
****** Inner Exception - END ******
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Error - END ~~~~~~~~~~~~~~~~~~~~~~~~~~
The code is pretty simple:
public virtual T Add(T entity)
{
try
{
using (U entityContext = new U())
{
T addedEntity = Add(entity, entityContext);
entityContext.SaveChanges();
return addedEntity;
}
}
catch (DbEntityValidationException e)
{
var msg = FormatDbValidationErrors(e);
throw new Exception(msg);
}
catch (Exception ex)
{
AmanLog.Logger.Error(ex);
throw;
}
}
public virtual T Update(T entity)
{
try
{
using (U entityContext = new U())
{
T existingEntity = Update(entity, entityContext);
entityContext.SaveChanges();
return existingEntity;
}
}
catch (DbEntityValidationException e)
{
var msg = FormatDbValidationErrors(e);
throw new Exception(msg);
}
catch (Exception ex)
{
AmanLog.Logger.Error(ex);
throw;
}
}
QueueUserWorkItemcode.