Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 数据库超时错误解决问题_.net_Asp.net_Vb.net_Ado.net - Fatal编程技术网

.net 数据库超时错误解决问题

.net 数据库超时错误解决问题,.net,asp.net,vb.net,ado.net,.net,Asp.net,Vb.net,Ado.net,嗨,我的应用程序中有一些数据库超时问题。由于某些网络故障,查询需要45秒以上的时间才能返回约10000行的结果集。大多数情况下,它的速度高达11-12秒。我的应用程序在后台作为计划作业运行 问题是,如果在将错误写入错误日志或退出之前出现超时异常,我需要尝试三次。我知道我可以将command timeout属性设置为60秒,但我仍然希望在退出之前尝试三次。我已经为此写了一个方法,但我认为我的方法有一些缺陷 现在,我专门将超时设置为5秒,因为我很清楚,运行这个存储过程需要25秒以上。我只想得到超时过

嗨,我的应用程序中有一些数据库超时问题。由于某些网络故障,查询需要45秒以上的时间才能返回约10000行的结果集。大多数情况下,它的速度高达11-12秒。我的应用程序在后台作为计划作业运行

问题是,如果在将错误写入错误日志或退出之前出现超时异常,我需要尝试三次。我知道我可以将command timeout属性设置为60秒,但我仍然希望在退出之前尝试三次。我已经为此写了一个方法,但我认为我的方法有一些缺陷

现在,我专门将超时设置为5秒,因为我很清楚,运行这个存储过程需要25秒以上。我只想得到超时过期的异常,并编写处理它的方法

我的问题是,如果在2次之后,我将连接超时设置为50,那么我的方法将运行并获得正确的列表,但它再次进入我在catch语句中调用的methodname中,最终什么也不返回。它看起来像是进入了递归或其他什么,但我不知道我错在哪里

任何提示或指针都将有助于澄清我的概念

Private Shared timeoutcounter As Integer = 0
Private Shared dbConnectionString As String = ConfigurationManager.AppSettings("DBConn")

Public Shared Function GetPurgeList() As DestList

    Dim AcInfo As DestInfo = Nothing
    Dim tempList As DestList= Nothing
    Dim ConnectionString As String = dbConnectionString
    Dim cn As New SqlConnection(ConnectionString)
    Dim cmd As SqlCommand = Nothing
    Dim dr As SqlDataReader = Nothing

    Try
        cn.Open()
        cmd = New SqlCommand
        With cmd
            .Connection = cn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "usp_abcd"
            .CommandTimeout = 5
            dr = .ExecuteReader
            If dr.HasRows Then
                tempList = New DestList()
                While dr.Read()
                    If dr.GetName(0).ToString() = "errnum" Then
                        ErrorLogger.WriteToErrorLog("Error from stored proc usp_abcd" + CStr(dr("errnum")), dr("errmsg"))
                    Else
                        AcInfo = New DestInfo
...
//fill object
...
                           tempList.Add(AccountInfo)
                    End If
                End While
            End If
        End With

    Catch ex As Exception
       If ex.Message.ToUpper.Contains("Timeout Expired".ToUpper) Then
            timeoutcounter = timeoutcounter + 1
            If timeoutcounter > 2 Then
                timeoutcounter = 0
                ErrorLogger.WriteToErrorLog("Exception from method GetPurgeList timeoutCounter > 3 : " + ex.Message, ex.StackTrace)
            Else
------------>  GetPurgeList() 'it gets here after getting the correct list and I am confused why does it come back here again and then finally return nothing
            End If
        Else
            ErrorLogger.WriteToErrorLog("Exception from method GetPurgeList : " + ex.Message, ex.StackTrace)

        End If

    Finally
        If dr IsNot Nothing Then
            dr.Close()
            dr = Nothing
        End If
        If cmd IsNot Nothing Then
            cmd.Dispose()
            cmd = Nothing
        End If
        If cn IsNot Nothing Then
            cn.Close()
            cn.Dispose()
            cn = Nothing
        End If
    End Try
    Return tempList
End Function
改变

GetPurgeList() 'it gets here after getting the correct list and I am confused why does it come back here again and then finally return nothing
进入

tempList = New DestList()

tempList = GetPurgeList()

从异常调用时,您没有保存GetPurgeList的返回值。我的建议是完全放弃递归。创建一个循环,将datareader部分提取到它自己的函数中,并在存储过程成功时调用它

下面我展示的不是我推荐的,而是为了展示一般流程。如果不重新打开连接,我不确定它是否能工作。如果我是为自己这样做的,我会将GetPurgeList中的几乎所有内容放入一个循环中,该循环在X迭代后终止,或者tempList不是空的(实际上这样写会更容易,但我希望这能让想法更清楚)


如果你投了-ve一票,也请留下一条评论,说明原因。使用一些断点来更好地识别错误并发布。谢谢你的详细解释。我现在明白了。
Private Shared timeoutcounter As Integer = 0
Private Shared dbConnectionString As String = ConfigurationManager.AppSettings("DBConn")

Private Shared Sub ReadFromDataReader(ByVal dr as DataReader, ByRef tempList as DestList)
        If dr IsNot Nothing AndAlso dr.HasRows Then
            tempList = New DestList()
            While dr.Read()
                Dim AcInfo As DestInfo = Nothing
                If dr.GetName(0).ToString() = "errnum" Then
                    ErrorLogger.WriteToErrorLog("Error from stored proc usp_abcd" + CStr(dr("errnum")), dr("errmsg"))
                Else
                    AcInfo = New DestInfo
...
//fill object
...
                       tempList.Add(AccountInfo)
                End If
            End While
        End If

End Sub

Public Shared Function GetPurgeList() As DestList

Dim tempList As DestList= Nothing
Dim ConnectionString As String = dbConnectionString
Dim cn As New SqlConnection(ConnectionString)
Dim cmd As SqlCommand = Nothing
Dim dr As SqlDataReader = Nothing

try
   cn.Open()
    cmd = New SqlCommand
    With cmd
        .Connection = cn
        .CommandType = CommandType.StoredProcedure
        .CommandText = "usp_abcd"
    End With
Catch ex as exception
  return Nothing ' possibly rethrow error here instead, in any case, do not continue 
End Try


Try
        cmd.CommandTimeout = 5
        dr = cmd.ExecuteReader
        ReadFromDataReader(dr, tempList)

Catch ex As Exception
   If ex.Message.ToUpper.Contains("Timeout Expired".ToUpper) Then
        try
           cmd.CommandTimeout = 15
           dr = cmd.ExecuteReader
           ReadFromDataReader(dr, tempList)
        Catch ex1 as Exception
           If ex.Message.ToUpper.Contains("Timeout Expired".ToUpper) Then
              Try
                cmd.CommandTimeout =25
                dr = cmd.ExecuteReader
                ReadFromDataReader(dr, tempList)

              Catch ex As Exception
                ErrorLogger.WriteToErrorLog("Exception from method GetPurgeList : " + ex.Message, ex.StackTrace)

          End Try
          Else
            ErrorLogger.WriteToErrorLog("Exception from method GetPurgeList : " + ex.Message, ex.StackTrace)


          End If
        End Try
    End If

Finally
    If dr IsNot Nothing Then
        dr.Close()
        dr = Nothing
    End If
    If cmd IsNot Nothing Then
        cmd.Dispose()
        cmd = Nothing
    End If
    If cn IsNot Nothing Then
        cn.Close()
        cn.Dispose()
        cn = Nothing
    End If
End Try
Return tempList
End Function