I have started to receive an error with my VB.NET application:
Timeout Expired. The timeout elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
I did have a few methods that were not correctly disposing of the connection. These have been fixed using Try and Finally.
However, this morning I received the error again whilst trying to open a simple report. I had MySQL Workbench open monitoring client connections. At the time I had 4 threads connected.
The MySQL DB kills connections that have been asleep for more than 15 seconds.
I am at a loss as to how I could have reached the max pool size, if indeed that is what the error is referring to.
Does the application read the max pool size setting and hold its own count of connections and throw the error when that number is reached, or does it get the number from the MySQL DB directly every time a new connection is opened?
Or could the error be due to something else?
EDIT 1
Some stats from MySQL Workbench
Threads Connected:3
Threads Running: 1
Threads Created: 250
Threads Cached: 5
Rejected (over limit): 0
Total Connections: 2822
Connection limit: 151
Aborted Clients: 2694
Aborted Connections: 84
Errors: 0
EDIT 2
Sample code calling and disposing of connection:
Public Shared Function GetCoursePaperCertificate(ByVal CourseTypeID As Integer) As String
Dim connx As New MySqlConnection(My.Settings.BMConnString)
Try
Dim cmdTextx = "Select `Cert` From `Courses` WHERE `ID`=@ID"
Dim cmdx As New MySqlCommand(cmdTextx, connx)
cmdx.Parameters.AddWithValue("@ID", CourseTypeID)
connx.Open()
Dim result = cmdx.ExecuteScalar
If result Is Nothing OrElse result Is DBNull.Value Then
Return String.Empty
Else
Return result
End If
Catch ex As Exception
Return String.Empty
Finally
connx.Dispose()
connx = Nothing
End Try
End Function
Using-statement wherever you use a connection. That means, use them only in a method scope and never make them shared. - Tim Schmelterusingwhen working with db connections to avoid problems with any connection that you might leave open. By the way, the problem could be caused by any proccess that takes too long and is using atransactionto lock up any table. Did you checked if there is any proccess that might be locking up the table that you are trying to use? - AbnerusingThere does appear to be one thread that is open and sleeping longer than it should. But, I would have thought that with 151 possible connections, this wouldn't be the culprit itself? - Dave B