ADO.Net上的SQLException SqlCommand.EndExecuteReader

时间:2013-10-29 13:54:04

标签: c# sql-server asynchronous ado.net

我正试图通过ADO.Net SqlCommand在一个连接上多次调用异步调用存储过程。

每隔半秒就会在一个计时器上触发调用,有时我会按预期收到结果,有时我收到以下错误:

  

System.Data.SqlClient.SqlException(0x80131904):发生严重错误   当前命令。如果有的话,结果应该被丢弃   在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolea   n breakConnection)
  在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常   ,Boolean breakConnection)
  在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
  在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cm)   dHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,Tds   ParserStateObject stateObj)
  在System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
  在System.Data.SqlClient.SqlDataReader.get_MetaData()
  在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,Run   行为runBehavior,String resetOptionsString)
  在System.Data.SqlClient.SqlCommand.CompleteAsyncExecuteReader()
  在System.Data.SqlClient.SqlCommand.InternalEndExecuteReader(IAsyncResult asy)   ncResult,String endMethod)
  在System.Data.SqlClient.SqlCommand.EndExecuteReader(IAsyncResult asyncResult)   )

SQL日志重复显示以下错误:

  

服务器将断开连接,因为客户端驱动程序在会话处于单用户模式时发送了多个请求。当客户端在会话中仍在运行批处理时发送重置连接的请求时,或者在会话重置连接时客户端发送请求时,会发生此错误。请联系客户端驱动程序供应商。

我的连接字符串有MARS和Async = true设置。我目前正在使用SQL Server 2008 Express,尽管目标客户端将是一个完全成熟的SQL Server实例。

我创建了以下控制台应用程序,它在我的机器上表现出相同的行为,我创建的DummySp只在其调用后立即返回

public class BusinessObject
{
    public string  Name {get; set;}
    public void UpdateData(DataTable dataTable)
    {
        Console.WriteLine("{0}: new data received.",Name);
    }
}

public class Program
{
    private const string SpName = "DummySp";
    private const string ConnectionsString = @"Data Source=(local)\sqlexpress;Initial Catalog=Test;Integrated Security=SSPI;Connection Timeout=3600";

    private static readonly object DbRequestLock = new object();
    private static readonly ManualResetEvent DatabaseRequestsComplete = new ManualResetEvent(false);
    private static int _databaseRequestsLeft;
    private static Timer _timer;

    static readonly List<BusinessObject> BusinessObjects = new List<BusinessObject>
    {
        new BusinessObject{Name = "A"},
        new BusinessObject{Name = "B"},
        new BusinessObject{Name = "C"},
    };

    static void Main(string[] args)
    {
        _timer = new Timer(DoQuery, null, 0, 500);

        Console.ReadLine();

        _timer.Dispose();         
    }

    private static void DoQuery(object state)
    {
        try
        {
           lock (DbRequestLock)
            {

                DatabaseRequestsComplete.Reset();
                _databaseRequestsLeft = BusinessObjects.Count;

                var builder = new SqlConnectionStringBuilder(ConnectionsString)
                    {
                        AsynchronousProcessing = true,
                        MultipleActiveResultSets = true
                    };

                using (var connection = new SqlConnection(builder.ConnectionString))
                {
                    connection.Open();

                    foreach (var businessObject in BusinessObjects)
                    {
                        var command = new SqlCommand(SpName, connection) { CommandType = CommandType.StoredProcedure };

                        command.BeginExecuteReader(Callback, new Tuple<SqlCommand, BusinessObject>(command, businessObject));
                    }

                    // need to wait for all to complete before closing the connection
                    DatabaseRequestsComplete.WaitOne(10000);
                    connection.Close();
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Following error occurred while attempting to update objects: " + ex);
        }
    }

    private static void Callback(IAsyncResult result)
    {
        try
        {
            var tuple = (Tuple<SqlCommand, BusinessObject>)result.AsyncState;
            var businessObject = tuple.Item2;

            using (SqlCommand command = tuple.Item1)
            {
                using (SqlDataReader reader = command.EndExecuteReader(result))
                {
                    using (var table = new DataTable(businessObject.Name))
                    {
                        table.Load(reader);

                        businessObject.UpdateData(table);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        finally
        {
            // decrement the number of database requests remaining and, if there are 0 fire the mre
            if (Interlocked.Decrement(ref _databaseRequestsLeft) == 0)
            {
                DatabaseRequestsComplete.Set();
            }
        }
    }
}

关于如何克服这个问题的任何想法?

由于

1 个答案:

答案 0 :(得分:1)

这不是直接回答我的问题,所以我没有标记它,但我认为值得展示每个对象有一个连接的替​​代方案,因为这似乎可以绕过这个问题......

 private static void DoQuery(object state)
    {
        try
        {
            lock (DbRequestLock)
            {

                var builder = new SqlConnectionStringBuilder(ConnectionsString)
                    {
                        AsynchronousProcessing = true,
                    };

                DatabaseRequestsComplete.Reset();
                _databaseRequestsLeft = BusinessObjects.Count;

                foreach (var businessObject in BusinessObjects)
                {
                    var newConnection = new SqlConnection(builder.ConnectionString);
                    newConnection.Open();

                    var command = new SqlCommand(SpName, newConnection) { CommandType = CommandType.StoredProcedure };

                    command.BeginExecuteReader(Callback, new Tuple<SqlCommand, BusinessObject>(command, businessObject),CommandBehavior.CloseConnection);

                }
                // need to wait for all to complete                    DatabaseRequestsComplete.WaitOne(10000);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Following error occurred while attempting to update objects: " + ex);
        }
    }

    private static void Callback(IAsyncResult result)
    {
        var tuple = (Tuple<SqlCommand, BusinessObject>)result.AsyncState;
        var businessObject = tuple.Item2;
        SqlCommand command = tuple.Item1;
        try
        {
            using (SqlDataReader reader = command.EndExecuteReader(result))
            {
                using (var table = new DataTable(businessObject.Name))
                {
                    table.Load(reader);

                    businessObject.UpdateData(table);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        finally
        {
            // decrement the number of database requests remaining and, if there are 0 fire the mre
            if (Interlocked.Decrement(ref _databaseRequestsLeft) == 0)
            {
                DatabaseRequestsComplete.Set();
            }

            try
            {
                command.Dispose();
                command.Connection.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }