问题
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!
回答1:
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
show processlist;
To see the current connections and
show global status;
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,
来源:https://stackoverflow.com/questions/8617824/closing-pooling-mysql-odbc-connections