加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net奇怪的异常每隔三分钟左右

发布时间:2020-12-16 09:36:33 所属栏目:asp.Net 来源:网络整理
导读:我试图创建一个非常简单的aspx页面,从 mysql数据库中提取一些数据. 页面构建没有问题. (aspx只包含默认表单和div只是为了打印一些数据) Default.aspx.vb: Imports System.ConfigurationImports System.DataImports MySqlImports MySql.DataImports MySql.Da
我试图创建一个非常简单的aspx页面,从 mysql数据库中提取一些数据.
页面构建没有问题.
(aspx只包含默认表单和div只是为了打印一些数据)

Default.aspx.vb:

Imports System.Configuration
Imports System.Data
Imports MySql
Imports MySql.Data
Imports MySql.Data.MySqlClient

Partial Class _Default
     Inherits System.Web.UI.Page

    Private cnstr As String =     ConfigurationManager.ConnectionStrings.Item("thisdb").ConnectionString

    Protected Sub Page_Load(sender As Object,e As System.EventArgs) Handles Me.Load

        Dim cn As New MySqlConnection(cnstr)
        Dim cmd As New MySqlCommand("SELECT user_firstname,user_lastname FROM tb_users;",cn)
        cmd.CommandType = CommandType.Text
        Dim dt As DataTable
        dt = GetDataTableMySQL(cmd)
        If dt.Rows.Count > 0 Then
            testdiv.InnerHtml = dt.Rows(0).Item("user_firstname")
            testdiv.InnerHtml += "<br/>" & dt.Rows(0).Item("user_lastname")
        End If
        dt.Dispose()
        cmd.Dispose()
        cn.Dispose()

    End Sub

    Private Function GetDataTableMySQL(ByVal cmd As MySqlCommand) As DataTable
        Dim da As New MySqlDataAdapter()
        Dim dt As New DataTable()
        Try
           cmd.Connection.Open()
           da.SelectCommand = cmd
           da.Fill(dt)
           Return dt
        Catch ex As MySqlException
            Throw ex
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            cmd.Connection.Close()
            da.Dispose()
       End Try
    End Function

End Class

Web.config文件:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application,please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0">
      <assemblies>
        <add assembly="MySql.Data,Version=6.4.4.0,Culture=neutral,PublicKeyToken=C5687FC88969C44D"/>
      </assemblies>
    </compilation>
    <customErrors mode="Off"/>
  </system.web>
  <connectionStrings>
    <add name="thisdb" connectionString="Server=localhost;Database=mydatabase;Uid=mydbuser;Pwd=dbpasswd;CharSet=UTF8; "/>
  </connectionStrings>
</configuration>

当我浏览页面URL时,它运行得很好.
如果我继续刷新页面,一切都没有问题.

现在出现了烦人的问题……

如果我让页面空闲大约3-4分钟,然后点击刷新我总是得到以下异常:

The given key was not present in the dictionary.

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Exception: The given key was not present in
the dictionary.

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.

Stack Trace:

[Exception: The given key was not present in the dictionary.]
_Default.GetDataTableMySQL(MySqlCommand cmd) +236 _Default.Page_Load(Object sender,EventArgs e) +112 System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)
+2207

Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.272

如果我再次点击刷新,页面再次正常运行……依此类推.

在网上搜索了几个小时后,唯一听起来就是这样
与我的问题有关,与mysql的Connector / Net有关:

Connector / Net文档说:

Starting with MySQL Connector/Net 6.2,there is a background job that runs every three
minutes and removes connections from pool that have been idle (unused) for more than three minutes.
The pool cleanup frees resources on both client and server side. This is because
on the client side every connection uses a socket,and on the server side every
connection uses a socket and a thread.

Prior to this change,connections were never removed from the pool,
and the pool always contained the peak number of open connections.
For example,a web application that peaked at 1000 concurrent database
connections would consume 1000 threads and 1000 open
sockets at the server,without ever freeing up those resources from the connection pool.

Note,connections,no matter how old,will not
be closed if the number of connections in the pool is less than or equal to the value
set by the Min Pool Size connection string parameter.

好.即使这是我的问题,
哪种连接方式正确 – >获取数据 – >断开连接?

有任何想法吗?这真让我抓狂!

UPDATE
在@Andrews的建议之后,我更改了函数“GetDataTableMySQL”,如下所示:

Private Function GetDataTableMySQL(ByVal cmd As MySqlCommand) As DataTable
    Dim dt As New DataTable
    Using da = New MySqlDataAdapter(cmd)
        da.Fill(dt)
    End Using
    Return dt
End Function

(它没有解决问题,但我认为现在展示代码看起来很有用)

异常的堆栈跟踪更改为以下内容:

[KeyNotFoundException: The given key was not present in the
dictionary.] System.Collections.Generic.Dictionary`2.get_Item(TKey
key) +9624829
MySql.Data.MySqlClient.CharSetMap.GetCharacterSet(DBVersion version,
String CharSetName) +23
MySql.Data.MySqlClient.CharSetMap.GetEncoding(DBVersion version,
String CharSetName) +47
MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection)
+510 MySql.Data.MySqlClient.MySqlConnection.Open() +418 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset,
DataTable[] datatables,Int32 startRecord,Int32 maxRecords,String
srcTable,IDbCommand command,CommandBehavior behavior) +123
System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables,Int32
startRecord,CommandBehavior
behavior) +166 System.Data.Common.DbDataAdapter.Fill(DataTable
dataTable) +115 _Default.GetDataTableMySQL(MySqlCommand cmd) +86
_Default.Page_Load(Object sender,Boolean includeStagesAfterAsyncPoint)
+2207

更新2阅读Connector/Net Connection String Options Reference后
我用选项测试了我的连接字符串:

Pooling=false;

然后我测试了将池化选项更改为:

Pooling=no;

通过测试Pooling选项,页面永远不会有效!
我每次都得到例外“字典中没有给定的密钥”.

解决方法

它可能无法解决问题,但对于DataAdapter.Fill您不需要自己打开和关闭连接.

另外,我怀疑在Try块中使用Return – 你可以做更多的事情

Private Function GetDataTableMySQL(ByVal cmd As MySqlCommand) As DataTable
    Dim dt As DataTable = Nothing
    Using da = New MySqlDataAdapter()
        da.fill(dt)
    End Using
    Return dt
End Function

并检查返回值IsNot Nothing是否确保它有效.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读