SQL错误:当前命令发生严重错误。结果(如果有的话)应该被丢弃

时间:2016-10-17 18:31:04

标签: sql-server multithreading

我正在使用VS Studio 2105 / SSMS 2014 v 12.0.410。使用VB.net。

我正在运行一个控制台应用程序,它在0秒和30秒标记上进行轮询。它将在24到7之间执行此操作。作为该过程的一部分,将生成后台线程。 (见下面的解释)。

数据库连接字符串是:

add key="ConnectionString" value="Server=xxxxx,14334;Database=xxx_dev;Uid=xxx_dev;Password=xxxxxx;MultipleActiveResultSets=true"

要求打开和关闭的类及其函数:

Dim strConnectionString As String = ConfigurationManager.AppSettings("ConnectionString")

Sub OpenDB()
    If objConn.State = Data.ConnectionState.Open Then
        objConn.Close()
    End If

    objConn.ConnectionString = strConnectionString
    objConn.Open()
    objCmd.Connection = objConn

    objCmd.CommandType = Data.CommandType.Text
End Sub

Sub CloseDB()
    objConn.Close()
End Sub

enter image description here

我自定义了错误消息,以便尽我所能。

  

当前命令发生严重错误。结果(如果有的话)应该被丢弃。 - >状态:0 - >来源:.Net SqlClient数据提供者 - >错误号码:0 - >行号:0 - >行号:-2146232060 - >分类:11 - >程序: - >调用堆栈:位于System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action`1 wrapCloseInAction)的System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection,Action`1 wrapCloseInAction)。系统上的System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean& dataReady)中的Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)。 System.SData.SqlClient.SqlCommand.RunExecuteReaderTds中的Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString,Boolean isInternal,Boolean forDescribeParameterEncryption)(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 timeout,Task& ; task,布尔asyn System.Data.SqlClient.SqlCommand.RunExecuteReader中的cWrite,Boolean inRetry,SqlDataReader ds,Boolean describeParameterEncryptionRequest)(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法,TaskCompletionSource`1完成,Int32超时,任务&任务,布尔& usedCache,Boolean asyncWrite,Boolean inRetry)在System.Data.SqlClient的System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1完成,String methodName,Boolean sendToPipe,Int32 timeout,Boolean& usedCache,Boolean asyncWrite,Boolean inRetry)中。 SqlCommand.ExecuteNonQuery()在ReliableSiteBandwidthPollingTester.Module1.SaveBandwidthPollingLog(Switch SaveSwitch,String& strMessage)C:\ Dans \ Work 2 \ Working Area \ Switch Polling - design 3 \ Tester apps \ ReliableSiteBandwidthPollingTester \ ReliableSiteBandwidthPollingTester \ Module1.vb:line 427

投票过程:

它询问收集数据的网络交换机。在询问之后,它然后执行105次简单的存储过程,每次将一行插入表中。然后它启动一个后台线程,该线程执行一个“计算过程”存储过程,该过程使用刚刚插入的表以及其他表。它确实插入另一个表 - 并可能删除。使用交易。它要么提交要么回滚。但是,我认为该程序在下一次民意调查开始之前没有完成 - 接下来的30秒标记。

简单的插入存储过程已经多次独立测试,并且每次都能正常工作。

“计算过程”存储过程经过多次独立测试,每次都能正常工作。

我启动控制台应用程序,它轮询3次,失败并显示错误。在失败之前,它会将3组105行写入表中。作为流程的一部分,它还将290行插入另一个表中。

enter image description here

enter image description here

   Sub PollSwitch(sender As Object, e As ElapsedEventArgs)

    Dim strMessage As String = ""
    Dim strPollError As String = ""
    Dim bPollResult As Boolean
    Dim NetworkSwitch As Switch

    ' This is the format to display as it is how it is stored in the info log table.
    Dim dtFormat As String = "yyyy-MM-dd hh:mm:ss.fff"

    If bErrorInThread = True Then
        ' There was an error in the background thread during the previous issuance of the thread. So do not continue.
        strMessage = "Critical Error - in the background thread. See the 'BandwidthInfoLog' table. Refer to this log date: " + dtThreadStartDateTime.ToString(dtFormat)
    End If

    If strMessage = "" Then
        ' Create a new instance.
        NetworkSwitch = New Switch(strCommandLineSwitchIP, strCommandLineCommunityString)

        Try
            ' Do the switch polling.
            bPollResult = NetworkSwitch.Poll(strPollError)

            If bPollResult = False Then
                strMessage = "Warning - in bandwidth poll. Poll error at " & Now & ": " & strPollError & " Poll will continue."
            Else
                ' Save to the bandwidth polling log.
                SaveBandwidthPollingLog(NetworkSwitch, strMessage)

                If InStr(strMessage, "Critical") = 0 Then
                    Console.WriteLine("Successfully polled at " & Now() & ".")

                    ' Set the date/time as it will be used above in an error message should the thread process fail.
                    dtThreadStartDateTime = Now()

                    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                    ' Now do the "calculation process" - it will be in it's own background thread.
                    ' Note: the thread ends when the ProcessCalculatedSwitchPolling method ends.
                    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                    Dim thread As New Thread(AddressOf ProcessCalculatedSwitchPolling)
                    thread.Start()
                End If
            End If
        Catch ex As Exception
            strMessage = "Critical Error - in bandwidth poll - " & ex.Message & "<br /><br />Switch: " & strCommandLineSwitchIP
        End Try
    End If

    If InStr(strMessage, "Critical") > 0 Then
        ' Stop this method (this poll timer) as there is a critical error.
        DisposeTimer()

        ' Show message.
        Console.WriteLine(strMessage)
        Console.WriteLine("--------->>>> PRESS ENTER TO QUIT.")
        Console.ReadKey()

        ' Exit the console application here as there is a critical error.
        ' Normally the exit would occur in the main method when the User hits a key.
        Environment.Exit(0)
    End If
End Sub

调用堆栈显示它在第427行失败,这是.ExecuteNonQuery()行。

   Sub SaveBandwidthPollingLog(ByVal SaveSwitch As Switch, ByRef strMessage As String)
    Const strFunctionId As String = "VB - savebandwidthpollinglog. Error ID: "
    Const iSQLErrorId As Integer = 501
    Const iCATCHErrorId As Integer = 502

    Dim dtCurrentDateTime As Date = Now()
    Dim strProcessInfoLogResult As String = ""
    Dim strAdditionalInfoForLog As String = ""

    ' This is the format to display as it is how it is stored in the info log table.
    Dim dtFormat As String = "yyyy-MM-dd hh:mm:ss.fff"

    DBFunc.OpenDB()

    Try
        With DBFunc.objCmd
            .CommandType = Data.CommandType.StoredProcedure
            .CommandText = "InsertBandwidthLogTest6"
            ' Note: it was timing out, so the SqlCommand.CommandTimeout property has expired; the default timeout is 30 seconds.
            .CommandTimeout = 0

            For Each SavePort In SaveSwitch.Port
                .Parameters.Clear()
                .Parameters.AddWithValue("@SwitchIP", SaveSwitch.IP)
                .Parameters.AddWithValue("@PortIndex", SavePort.Index)
                .Parameters.AddWithValue("@PortSpeed", SavePort.Speed)
                .Parameters.AddWithValue("@InOctets", SavePort.InOctets)
                .Parameters.AddWithValue("@OutOctets", SavePort.OutOctets)
                .Parameters.AddWithValue("@TimeStamp", SavePort.TimeStamp)

                .ExecuteNonQuery()
            Next
        End With
    Catch sqlex As SqlException
        ' Its a critical issue.
        If InStr(sqlex.Message, "Critical") > 0 Then
            ' Coming from the stored procedure.
            strMessage = sqlex.Message
        Else
            strProcessInfoLogResult = ProcessInfoLog(dtCurrentDateTime, sqlex.Message + " --> State: " + sqlex.State.ToString() + " --> Source: " + sqlex.Source + " --> Error number: " + sqlex.Number.ToString() + " --> Line number: " + sqlex.LineNumber.ToString() + " --> Line number: " + sqlex.HResult.ToString() + " --> Class: " + sqlex.Class.ToString() + " --> Procedure: " + sqlex.Procedure + " --> Call stack: " + sqlex.StackTrace, strAdditionalInfoForLog, strFunctionId, iSQLErrorId)
        End If
    Catch ex As Exception
        strProcessInfoLogResult = ProcessInfoLog(dtCurrentDateTime, ex.Message, strAdditionalInfoForLog, strFunctionId, iCATCHErrorId)
    Finally
        ' Close database.
        DBFunc.CloseDB()
    End Try
End Sub

线程过程:

    Public Sub ProcessCalculatedSwitchPolling()
    Const strFunctionId As String = "VB - processcalculatedswitchpolling. Error ID: "
    Const iSQLErrorId As Integer = 601
    Const iCATCHErrorId As Integer = 602

    Dim strProcessInfoLogResult As String = ""
    Dim strAdditionalInfoForLog As String = ""
    Dim strInputParms As String = ""

    ' Set for the error log.
    strInputParms = "S/P parmameters - switch IP Address: " & strCommandLineSwitchIP

    DBFunc.OpenDB()

    Try
        With DBFunc.objCmd
            .CommandType = Data.CommandType.StoredProcedure
            .CommandText = "ProcessBandwidthLogCalculatedTest6"
            ' Note: it was timing out, so the SqlCommand.CommandTimeout property has expired; the default timeout is 30 seconds.
            .CommandTimeout = 0
            .Parameters.Clear()

            .Parameters.AddWithValue("@SwitchIP", strCommandLineSwitchIP)

            .ExecuteNonQuery()
        End With
    Catch sqlex As SqlException
        ' Its a critical issue.
        If InStr(sqlex.Message, "Critical") > 0 Then
            ' Coming from the stored procedure.
            ' Set the global variable - the thread error indicator.
            bErrorInThread = True
        Else
            ' Not coming from the stored procedure.
            bErrorInThread = True
            ' Log the exception as it was not logged in the stored procedure.
            strAdditionalInfoForLog = strInputParms
            strProcessInfoLogResult = ProcessInfoLog(dtThreadStartDateTime, sqlex.Message, strAdditionalInfoForLog, strFunctionId, iSQLErrorId)
        End If
    Catch ex As Exception
        bErrorInThread = True
        ' Log the exception.
        strAdditionalInfoForLog = strInputParms
        strProcessInfoLogResult = ProcessInfoLog(dtThreadStartDateTime, ex.Message, strAdditionalInfoForLog, strFunctionId, iCATCHErrorId)
    Finally
        ' Close database.
        DBFunc.CloseDB()
    End Try
End Sub

0 个答案:

没有答案