1

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 of db.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.

7
  • 2
    Are you sure that the connections are being leaked, or that you're asking for a connection while the pool is full? This can happen if the time it takes for you to close a connection takes long.
    – Matthew
    Commented Nov 22, 2013 at 18:40
  • I'm not sure they are leaked, I just supposed because of the exception, so that is a possibility, I will investigate. Commented Nov 22, 2013 at 18:49
  • Is it possible that sometimes exceptions are thrown and caught?your code does not close the connection if an exception happens (hint: use "using") Commented Nov 22, 2013 at 18:55
  • @MarcGravell I've checked that. Actually I was using using before, and changed to the explicit Connection.Close() to try to solve the problem. Commented Nov 22, 2013 at 19:00
  • 1
    @AndréSantaló database blocking; sp_who2 etc Commented Nov 22, 2013 at 21:22

4 Answers 4

1

It's probably just connection pooling. It's very expensive to initiate a new connection, so .Net will keep the connections open and re-use them.

5
  • Even if I'm constantly calling Connection.Close()? Commented Nov 22, 2013 at 18:57
  • That's correct. Connection pooling will not close the underlying connection.
    – Scottie
    Commented Nov 22, 2013 at 19:13
  • That's interesting. I think I have little knowledge about connection pooling, I will read about it. Commented Nov 22, 2013 at 19:34
  • I will mark this as the answer because the problem was exaclty that... the connection pool was growing larger and larger the more my thread run. I discovered this through debugging and running sp_who2. Commented Nov 25, 2013 at 17:12
  • 1
    @AndréSantaló, please make your own answer instead of modifying another's.
    – gunr2171
    Commented Nov 25, 2013 at 18:42
1

Do you have indexes on your tables? The large number of inserts would also cause the indexes to be updated which could adversely affect your select statements. Also the select statements may also be using the temp database to ensure that they are repeatable, again something that could be expensive. These may have a cumulative affect that is pushing up the time needed to complete what was a fast operation

1

It sounds to me like your query is taking an abnormal amount of time to run (i.e. greater than 10-15 seconds). In these cases the connection can time out. I would reccommend that you try running the query by itself, using any parameters that may be passed by the application, and if it is taking a long time to query, see if there is any way that you can improve the SQL.

You can always reference this: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated

0

Take a look at your methodA(), and tell me what you think will happen if an exception is thrown or the thread is aborted after creating the data context, but before closing it? The correct answer is that the connection will be left hanging open, at least of a while.

You must encase anything to do with database access in a try/finally block, such that the connection is closed as part of the finally. The easiest way to do this is with a using block. I know you said you tried that, but you should have posted that code:

void methodA()
{
    using (SIDataContext db = new SIDataContext(_host.Config.ConnectionString))
    {
        if (looksForSomeData())
            raiseSomeEvents();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.