EF引发异常:在提供程序连接上启动事务时发生错误。

问题描述:

您好

我间歇性地收到此错误,例外情况如下:

I am getting this error intermittently, the exception details are as follows:

异常类:  

   System.Data.SqlClient.SqlException

   System.Data.EntityException

Exception classes:  
   System.Data.SqlClient.SqlException
   System.Data.EntityException

异常消息:

  不允许新的交易,因为会话中还有其他线程。

  在提供程序连接上启动事务时发生错误。有关详细信息,请参阅内部异常。

Exception messages:
   New transaction is not allowed because there are other threads running in the session.
   An error occurred while starting a transaction on the provider connection. See the inner exception for details.

堆栈跟踪:

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,Boolean breakConnection)

  在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)

  在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)

   at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte [] buffer,TransactionManagerRequestType request,String transactionName,TransactionManagerIsolationLevel isoLevel,Int32 timeout,SqlInternalTransaction transaction,TdsParserStateObject
stateObj,Boolean isDelegateControlRequest)

&nbsp ; 在System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest,String transactionName,IsolationLevel iso,SqlInternalTransaction internalTransaction,Boolean isDelegateControlRequest)

  在System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso,String transactionName)

  在System.Data.SqlClient.SqlInternalConnection.BeginTransaction(IsolationLevel iso)

  在System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)

  在System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
$
---下一个呼叫堆栈:

  在System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)

  在System.Data.EntityClient.EntityConnection.BeginTransaction()

   at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)

Stack Traces:
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName)
   at System.Data.SqlClient.SqlInternalConnection.BeginTransaction(IsolationLevel iso)
   at System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)
   at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
--- Next Call Stack:
   at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
   at System.Data.EntityClient.EntityConnection.BeginTransaction()
   at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)

导致此异常的代码片段如下:

The code snippet that causes this exception is as follows:

public void AddThirdPartyForApplication(List< int> Applications ,int ThirdPartyUserId){

试试
  {

    if(Applications.Count< = 0 || ThirdPartyUserId< = 0) 

     return;

public void AddThirdPartyForApplication(List<int> Applications, int ThirdPartyUserId) {
try
 {
    if (Applications.Count <= 0 || ThirdPartyUserId <= 0) 
    return;

  &NBSP;&NBSP; foreach(在应用程序中的int ApplicationId) 

     {var tpts = from pt in MyDB.ApplicationThirdParty

      ;&NBSP;其中pt.OrganizationUser.User_Id == ThirdPartyUserId&& pt.Application.Application_Id == ApplicationId

     选择pt;

    foreach (int ApplicationId in Applications) 
    { var tpts = from pt in MyDB.ApplicationThirdParty
      where pt.OrganizationUser.User_Id == ThirdPartyUserId && pt.Application.Application_Id == ApplicationId
      select pt;

      ApplicationThirdParty thirdP = tpts.FirstOrDefault< ApplicationThirdParty>();  

      if(thirdP == null)//不存在

    {&NBSP; ApplicationThirdParty newThirdP = new ApplicationThirdParty();


       newThirdP.Application = MyDB.Proposal.Where(p => p.Application_Id == ApplicationId).FirstOrDefault();

       newThirdP.OrganizationUser = MyDB.OrganizationUser.Where(o => o.User_Id == ThirdPartyUserId).FirstOrDefault();

     newThirdP.Is_Active = true;      

     MyDB.AddToApplicationThirdParty(newThirdP);

  }

   else

      ; thirdP.Is_Active = true;   

      ApplicationThirdParty thirdP = tpts.FirstOrDefault<ApplicationThirdParty>();  
     if (thirdP == null) //does not exists
    {  ApplicationThirdParty newThirdP = new ApplicationThirdParty();
       newThirdP.Application = MyDB.Proposal.Where(p => p.Application_Id == ApplicationId).FirstOrDefault();
       newThirdP.OrganizationUser = MyDB.OrganizationUser.Where(o => o.User_Id == ThirdPartyUserId).FirstOrDefault();
     newThirdP.Is_Active = true;      
     MyDB.AddToApplicationThirdParty(newThirdP);
  }
  else
     thirdP.Is_Active = true;   

   }

   }

   MyDB.SaveChanges();

 }

   catch(例外情况)

   {

       ExceptionLogger.LogException(ex); 抛出新的DataAccessException(ex.Message," AddThirdPartyForApplication");


   }
}

   MyDB.SaveChanges();
 }
   catch (Exception ex)
   {
      ExceptionLogger.LogException(ex); throw new DataAccessException(ex.Message, "AddThirdPartyForApplication");
   }
}

看起来你有多个线程命中全局上下文。 我的建议是你将这个方法的局部内容拉出来。 上下文(及其下面的对象,例如statemanager)不是线程安全的。

It looks like you having multiple threads hitting a global context.  My suggestion would be you pull the context local to this method.  The context (and object underneath it, e.g. the statemanager) are not thread safe.

-Tim