我想做一个非常简单的aspx页面,从MySQL数据库中拉出一些数据。 页面生成没有问题。 (在ASPX只包含默认的形式和一个div只是打印一些数据)asp.net奇怪的例外每三分钟大约
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) +2207Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
如果我再次点击刷新,页面再次运行正常......等等。
后在网络上搜索的时间,这听起来 有关我的问题的唯一的事情,与MySQL的连接器/净做:
连接器/网络文件说:
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, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) +166 System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +115 _Default.GetDataTableMySQL(MySqlCommand cmd) +86
_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
更新2看完了Connector/Net Connection String Options Reference 我测试了我的连接字符串的选项:
Pooling=false;
,然后我测试改变池选项:
Pooling=no;
通过测试池选项,该网页从不起作用! 我每次都会得到一个异常“给定的密钥不在字典中”。
感谢您的帮助@安德鲁!我正在尝试你的建议!很奇怪,但是...我从来没有这个问题与** MS-SQL **和这个功能 – 2012-03-15 08:21:15
OK我改变了功能和测试...不幸的是,我得到了同样的异常“给定的键不存在于字典“,页面闲置3到4分钟后:( – 2012-03-15 08:33:42
我认为数据库可能不是问题,例如,请参阅http://stackoverflow.com/questions/552280/asp-net-mvc-the-给琴键是不在场,在最词典 – 2012-03-15 15:52:46