我正在尝试制作一个非常简单的 aspx 页面,它可以从 mysql 数据库中提取一些数据。 页面构建没有问题。 (aspx 仅包含默认表单和一个仅用于打印一些数据的 div)
默认.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
网络配置:
<?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 的连接器/网络有关:
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.
好的。即使这是我的问题, 连接-->获取数据-->断开连接的正确方法是什么?
有什么想法吗?这真让我发疯!
更新 根据@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;
通过测试 Pooling 选项,页面无法正常工作! 我每次都收到异常“字典中不存在给定的键”。
最佳答案
可能不能解决问题,但是DataAdapter.Fill不需要自己打开和关闭连接。
此外,我怀疑在 Try block 中使用 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 以确保其有效。
关于asp.net 大约每三分钟出现一次奇怪的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9710251/
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou
我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
SPI接收数据左移一位问题目录SPI接收数据左移一位问题一、问题描述二、问题分析三、探究原理四、经验总结最近在工作在学习调试SPI的过程中遇到一个问题——接收数据整体向左移了一位(1bit)。SPI数据收发是数据交换,因此接收数据时从第二个字节开始才是有效数据,也就是数据整体向右移一个字节(1byte)。请教前辈之后也没有得到解决,通过在网上查阅前人经验终于解决问题,所以写一个避坑经验总结。实际背景:MCU与一款芯片使用spi通信,MCU作为主机,芯片作为从机。这款芯片采用的是它规定的六线SPI,多了两根线:RDY和INT,这样从机就可以主动请求主机给主机发送数据了。一、问题描述根据从机芯片手