我们有一个处理XML文件的应用程序。该应用程序使用EF 6将数据保存到DB。应用程序使用ThreadPool.QueueUserWorkItem方法处理并发线程中的文件。该应用程序可以正常工作1.5年。 现在我们开始获得EF异常,如下所示:
在大多数情况下,没有这种错误,但是每天都会出现几次。我们没有成功找出导致EF引发此错误的原因或条件。
有什么建议吗?
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;
}
}
答案 0 :(得分:1)
应用程序使用ThreadPool.QueueUserWorkItem方法处理并发线程中的文件。
不能从两个线程中同时使用同一连接,否则:发生这种情况。要么:
lock
是实现此目的的简单方法)