关闭/池化 MySQL ODBC 连接
我正在重建一个在过去五年中开发的系统,最初是从经典 ASP 开始的,现在我将整个系统转移到 ASP.NET(使用 VB.NET)
该系统一直受到数据过多的持续问题的困扰连接打开,导致周期性的“max_connections is exceeded”错误。
在多次询问我的服务器主机这个问题后,我仍然遇到麻烦,所以我想我会把它开放给SO。
我当前打开连接如下:
Dim sql = "SQL SELECT"
Dim oConnection As OdbcConnection = New OdbcConnection(ConfigurationManager.ConnectionStrings("dbConn").ConnectionString)
oConnection.Open()
openDatabase = New OdbcCommand(sql, oConnection)
连接字符串包含在 web.config 文件中,看起来像这样
<add name="dbConn" connectionString="DRIVER={MySQL ODBC 3.51 Driver}; SERVER=mysql.dc-servers.com; DATABASE=dbName; UID=user; PASSWORD=pwd; OPTION=3; pooled=true" providerName="System.Data.Odbc"/>
我正在使用这样的 DataReader 获取实际数据:
Dim objDataReader As OdbcDataReader
objDataReader = openDatabase.ExecuteReader(CommandBehavior.CloseConnection)
While (objDataReader.Read())
// do stuff
End While
objDataReader.Close()
据我了解,假设数据或数据库没有错误(CommandBehavior.CloseConnection)
应确保当 objDataReader.Close()
行关闭 Reader 时,它也应关闭连接(或将其返回到池中)
我'我得到这些max_connections 错误。
查看 MySQL 管理上打开的进程,我可以看到连接没有被关闭。
恐怕我对连接过程了解甚少,对 MySQL 服务器的访问也非常有限,所以很难弄清楚这里发生了什么...当然,除非...我误解了一些东西,我希望是这样你们可以指点我一下!
I am rebuilding a system that has developed over the past five years, initially started with Classic ASP I am now moving the whole thing to ASP.NET (using VB.NET)
The system has been plagued by a persistent problem of having too many data connections open, resulting in periodic "max_connections is exceeeded" errors.
Having asked my server hosts many times about this, I am still having troubles so thought I'd open it up to SO.
I am currently opening connections as follows:
Dim sql = "SQL SELECT"
Dim oConnection As OdbcConnection = New OdbcConnection(ConfigurationManager.ConnectionStrings("dbConn").ConnectionString)
oConnection.Open()
openDatabase = New OdbcCommand(sql, oConnection)
The connection string is contained with the web.config file and looks like this
<add name="dbConn" connectionString="DRIVER={MySQL ODBC 3.51 Driver}; SERVER=mysql.dc-servers.com; DATABASE=dbName; UID=user; PASSWORD=pwd; OPTION=3; pooled=true" providerName="System.Data.Odbc"/>
I am getting actual data using a DataReader like this:
Dim objDataReader As OdbcDataReader
objDataReader = openDatabase.ExecuteReader(CommandBehavior.CloseConnection)
While (objDataReader.Read())
// do stuff
End While
objDataReader.Close()
It is my understanding that, assuming there are no errors with the data or database (CommandBehavior.CloseConnection)
should ensure that when the line objDataReader.Close()
closes the Reader, it should close the connection too (or return it to the pool)
I'm getting these max_connections errors though.
Looking at the open processes on MySQL admin, I can see that the connections are not being closed.
I have minimal understanding of the connection process I am afraid and very limited access to the MySQL server, so struggling to find out what is going on here... unless of course... I have misunderstood something, which I hope is the case and you guys can point me to it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 DataReader 了解不多,但看来您需要找到此泄漏。我建议首先使用以下方法在连接字符串中手动设置池大小:最大和最小池大小属性,请参阅了解详细信息:http://dev.mysql.com/doc/refman/5.0/en/connector-net-connection-options.html
我也建议监控与连接的打开和关闭池设置为关闭以查看连接生命周期实际发生的情况。您可以在 mysql 控制台中使用
来查看当前连接并
监控所有数据库属性。我还建议阅读 mysql 的这篇文章,解释“太多连接问题”
http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html
确保 mysql 配置与您的应用程序配置一致,以确保您这样做不允许太多连接服务器实际上允许。
干杯,
I do not know a lot about the DataReader but it seems as you need to find this leak. I would suggest first to manually set the pool size in your connection string using: Max and Min Pool Size attributes see for details: http://dev.mysql.com/doc/refman/5.0/en/connector-net-connection-options.html
I would also suggest monitoring the opening and closing of connection with the pooling set to off to see what actually happens to the connection lifecycle. You can use in the mysql console
To see the current connections and
To monitor all the db attributes. I would also suggest reading this article from mysql explaining the "too many connection problem"
http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html
Ensure that mysql configuration are in line with your application configuration to make sure that you do not allow too many connection than the server actually allows.
Cheers,