本文介绍了InvalidOperationException:已经有与此命令关联的打开的DataReader,必须首先关闭它.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我在生产环境中遇到上述异常.我已经搜索了例外,大多数情况下的解决方案是使用MARS.但是在连接字符串中添加了MARS之后,出现了以下异常:

Hi All

I am getting the above exception in production environment. I''ve googled the exception, and the solution in most cases is to use MARS. But after adding the MARS in the connection string, I got the following exception:

System.Data.SqlClient.SqlException: The transaction operation cannot be performed because there are pending requests working on this transaction.



但是目前我不是在寻找解决方案,而是希望你们中的任何一个都可以帮助我复制这个问题.无论在内部还是在UAT上,都无法在任何环境中复制此信息.突然冒出来.直到昨天,它运行良好.

当我提交事务时,会抛出以上异常.



But currently I''m not looking for the solution, rather if any of you can help me replicate this issue. This is not getting replicated in any environment neither in house, nor at UAT. came suddenly out of nowhere. Until yesterday it was running perfectly.

The above exceptions are thrown when I commit the transaction.

public string Subscribe(DateTime date)
{
    ABCDataContext dataContext = new ABCDataContext();
    return Process(RAKBankDataAccess.GetRegistration(dataContext, date), dataContext);
}

private string Process(IQueryable<DeliveryRegistration> registrations,
                       ABCDataContext dataContext)
{
        System.Data.Common.DbTransaction trans = null;
        if (dataContext.Connection.State != System.Data.ConnectionState.Open)
            dataContext.Connection.Open();
        trans = dataContext.Connection.BeginTransaction();
        dataContext.Transaction = trans;
        dataContext.CommandTimeout = 0;
        foreach (DeliveryRegistration dr in registrations)
        {
            //perform some modifications
            //Add some new rows

            transactionCount++;
           if (transactionCount == 250)
            {
                dataContext.SubmitChanges();
                dataContext.Transaction.Commit();
                if (dataContext.Connection.State!=
                    System.Data.ConnectionState.Open)
                    dataContext.Connection.Open();
                trans = dataContext.Connection.BeginTransaction();
                dataContext.Transaction = trans;

                transactionCount = 1;
            }

        }

        dataContext.SubmitChanges();
        dataContext.Transaction.Commit();

        return "";
}



数据库服务器是Sql Server 2005
谢谢



DB server is Sql Server 2005
Thanks

推荐答案



foreach (DeliveryRegistration dr in registrations)
       {
           //perform some modifications
           //Add some new rows

           transactionCount++;
          if (transactionCount == 250)
           {
               dataContext.SubmitChanges();
               dataContext.Transaction.Commit();
               if (dataContext.Connection.State!=
                   System.Data.ConnectionState.Open)
                   dataContext.Connection.Open();
               trans = dataContext.Connection.BeginTransaction();
               dataContext.Transaction = trans;

               transactionCount = 1;
           }

       }
       if(transactionCount % 250 != 0)
{
       dataContext.SubmitChanges();
       dataContext.Trans%action.Commit();
}

       return "";


这篇关于InvalidOperationException:已经有与此命令关联的打开的DataReader,必须首先关闭它.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 17:01