I have a DLL that is referenced by a Windows Service that has a number of jobs fired by timers throughout the day (different intervals per job, from 20 seconds to 15 minutes) however lately in my log file I have noticed lots of errors with this message:
SQL Error: Timeout expired. The timeout period 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 don't know whether there is some setting in SQL (2015) that I can change or look at or whether it's a code issue.
The code in the Service that calls the different jobs in the DLL (which has an SQLDatabase class) always calls Control.Dispose() after finishing which destroys all the objects including the SQLDatabase class which destroys the connection in it's own dispose method.
e.g after a timed job has run I dispose of it e.g
Control control = new Control();
control.RunJobs("SOME_JOB_TO_RUN");
control.Dispose();
Which then calls a method on my own main class Kill() which calls
this.DataAccess.Dispose();
And that should kill the object and close the connection e.g in my DataAccess object...
public class SqlDataAccess : System.IDisposable
public void Dispose()
{
if (_connection != null)
{
_connection.Close();
_connection.Dispose();
}
}
So a) I am not sure why these errors have started popping out of nowhere all of a sudden and I have increased the timer gaps to reduce the number of processes running to see if that helps (it hasn't).
Also as multiple processes could be running at the same time (due to timer overlaps), each call to the DLL from each method (called by the relevant timer), shouldn't (as far as I know - I could be wrong) be able to share connections or know about other processes going on at the same time.
So I am unsure whether I should or shouldn't be killing the DataAccess class and destroying the connection to allow for pooling or whether I should be doing something else.
As far as I was aware (like ASP classic), re-use of connections was the preferred method and we always used to open a connection to the DB at the earliest point (when required), re-use that connection throughout the page, and then destroy it at the end when no longer required.
However this is a Windows Service with multiple processes all being called at overlapping times with timers making use of a DLL that does the work. I am re-using the connections during each timed process call and destroying them at the end as you can see but maybe I should be doing something else.
Any help or ideas would be much appreciated.