I have a thread (called through new Thread(new ThreadStart(...))
) that is always running while my software is running that makes a polling in a database.
In a very simplyfied way, it basically does the following:
void Polling()
{
Thread.Sleep(500);
methodA();
if (certainTimePassed)
methodB();
if (anotherCertainTimePassed)
methodC();
...
}
void methodA()
{
SIDataContext db = new SIDataContext(_host.Config.ConnectionString);
if (looksForSomeData())
raiseSomeEvents();
db.Connection.Close();
}
It runs by hours and hours fine. However, if I stress the application by making the looksForSomeData() returning always true (i.e. constantly inserting the data it searches) AND at the same time use another software part that query large SELECTs at the database, it eventually produces the following (always in MethodA, probably because it's the one is most called):
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 think it may be some more complex problem. But since I have only a few months experience with LINQs DataContext() I'm not sure.
Things to note/I tried:
- Using
using
instead ofdb.Connection.Close()
- There is no way that it is opening the connection, raising an exception, and not closing the connection, because if an exception is raised in this thread the software quits
- The events raised in
raiseSomeEvents()
does not accumulate (probably), because there is some kind of semaphore solution that makes an event raised only it is not already running. - Catching the exception and doing nothing. The software slows down and even froze for some moments and then comes back to normal (seems the connections are eventually released).
I appreciate anything that could help me diagnose where the problem could be.
using
before, and changed to the explicit Connection.Close() to try to solve the problem.