我想做一個非常簡單的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