【问题标题】:ASP.NET page getting Timeout expired requiring the application pool being recycled every hourASP.NET 页面超时,要求每小时回收应用程序池
【发布时间】:2020-01-21 18:30:58
【问题描述】:

我的新申请页面在获得一些流量后每隔一小时左右就会出现一次超时错误,我所说的流量是指用户提交 5-10 个申请。如何找到连接被捆绑的原因?

这在过去一直是个问题,所以每当我使用 sql 数据读取器对象时,我都会确保实现“Using”语句。我还确保在处理数据读取器之前线程不会中止。我怀疑我对数据阅读器的使用是问题所在,所以也许是我的非查询代码导致了问题,但我不明白为什么。

我还为我的下拉列表控件使用了一些 sqldatasource 对象,据我所知,这不会是我的问题的根源。

查看代码示例了解我如何使用我的 sql 对象。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Using drApp As SqlDataReader = LookupAppByID()
            'some code
        drApp.Close()
    End Using
End Sub

Public Function LookupAppByID() As SqlDataReader
    Dim Command As New SqlClient.SqlCommand

    Command.Connection = GetDBConnection()
    Command.CommandType = CommandType.Text
    Command.CommandText = "select statement"

    Return Command.ExecuteReader(CommandBehavior.CloseConnection)
End Function

Public Function UpdateAppStatus() As Integer
    UpdateAppStatus = 0
    Using Command As New SqlClient.SqlCommand("update statement", GetDBConnection())
        UpdateAppStatus = Command.ExecuteNonQuery()

        Command.Connection.Close()
        Command.Connection.Dispose()
        Command.Dispose()
    End Using
End Function

Public Function GetDBConnection() As SqlClient.SqlConnection
    Dim connection As New SqlClient.SqlConnection
    connection.ConnectionString = "connection string"
    connection.Open()
    Return connection
End Function

显然,我希望它能够顺利运行,但是当用户开始点击该页面时,它会收到此错误:超时已过期。在从池中获取连接之前超时时间已过。这可能是因为所有池连接都在使用中并且已达到最大池大小。

我的代码有问题吗? 如何缩小导致此问题的原因?

【问题讨论】:

    标签: asp.net vb.net timeout sqlconnection application-pool


    【解决方案1】:

    我会将数据库对象保存在本地,以便确保它们已关闭和处置。在一种方法中创建和处置。我创建了一个类,因此您可以轻松地将其所有属性传递到一个变量中。

    'User Interface Code
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim id As Integer 'No sure where the value comes from
        Using dtApp As DataTable = DataAccess.LookupAppByID(id)
            'some code
        End Using
    End Sub
    
    Protected Sub Update_Click(sender As Object, e As EventArgs) Handles Update.Click
        Dim Applic As New ApplicationData()
        Applic.ApplicantsName = txtName.Text
        Applic.ID = CInt(txtID.Tex)
        Applic.ApplicationStatus = "Approved"
        Dim retVal = DataAccess.UpdateAppStatus(Applic)
        If retVal = 1 Then
            'Alert user of success
        Else
            'Alert user of failure
        End If
    End Sub
    
    Public Class ApplicationData
        Public Property ID As Integer
        Public Property AppDate As Date
        Public Property ApplicantsName As String
        Public Property ApplicationStatus As String
    End Class
    
    Public Class DataAccess
        Private Shared ConString As String = "Your connection string"
        Public Shared Function LookupAppByID(AppID As Integer) As DataTable
            Dim dt As New DataTable
            Using cn As New SqlConnection(ConString)
                Using Command As New SqlCommand("select * From YourTable Where ID = @ID", cn)
                    Command.Parameters.Add("@ID", SqlDbType.Int).Value = AppID
                    cn.Open()
                    dt.Load(Command.ExecuteReader)
                End Using
            End Using
            Return dt
        End Function
    
        Public Shared Function UpdateAppStatus(App As ApplicationData) As Integer
            Dim AppStatus = 0
            Using cn As New SqlConnection(ConString)
                Using Command As New SqlClient.SqlCommand("Update YourTable Set Status = @Status Where ID = @ID;", cn)
                    Command.Parameters.Add("@Status", SqlDbType.VarChar, 50).Value = App.ApplicationStatus
                    Command.Parameters.Add("@ID", SqlDbType.Int).Value = App.ID
                    cn.Open()
                    AppStatus = Command.ExecuteNonQuery()
                End Using
            End Using
            Return AppStatus
        End Function
    End Class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      • 2011-01-12
      • 2012-03-17
      • 2011-08-22
      • 2012-01-30
      • 2013-08-15
      相关资源
      最近更新 更多