tags:

views:

286

answers:

3

Greetings, I have to following problem. I have a WCF Service which runs under IIS7. Application connects to it and WCF Service makes some requests to DB. I have notice in activity monitor in SQL Server 2005 that after there are exactly 102 active connections, the application pool in IIS7 hangs. After this I can't connect to my WCF Service. Then only IIS7 restart helps. For connection I am using ChannelFactory, it's closed after each request. I've also introduced code like this to be sure that Channel is closed:

catch (FaultException)
{
    Factory.Abort();
    return null;
}
catch (CommunicationException)
{
    Factory.Abort();

    return null;
}
catch (TimeoutException)
{
    Factory.Abort();
    return null;
}
catch (Exception ex)
{
    Factory.Abort();
    return null;
}

finally
{
    Factory.Close();
    Factory.Abort();
}

I have also the following behvavior for my service class:

[ServiceBehavior(InstanceContextMode= InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple, AutomaticSessionShutdown=true)]

I have also the following in my service web.config file:

<serviceBehaviors>
<behavior name="Server.Service1Behavior">
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>

      <serviceThrottling maxConcurrentCalls="2147483647"
        maxConcurrentSessions="2147483647"
        maxConcurrentInstances="2147483647" />

I tried everything. Please help me because user's can't work like this. Why it's happen that after 102 connections to DB application pool hangs? Here is the code that calls on the database

internal SqlConnection CheckIfConnectionOpen()
    {
        if (_Connection.State != ConnectionState.Open)
        {
            _Connection.Open();
        }
        return _Connection;
    }
using (SqlCommand cmd = new SqlCommand(query, _Connection))
    { 
CheckIfConnectionOpen();
//some parameters for sqlcommand here and execute nonQuery or execute reader
}

Can someone please help me with this because I am still looking for a solution

+1  A: 

On a long shot the link below explains a situation when the app pool crashes it does not restart. If you can get it to restart that should decrease the severity of the issue.

http://i.nconspicuo.us/2008/06/25/iis7-on-windows-server-2008-503-service-unavailable-error-application-pool-stops-unexpectedly/

You mentioned you have checked the Channel is closed, it might be good to confirm the DB connections are closed too.

Hope this helps!

Russell
I have already set StartAutomatically and increased max number of fails. I didn't help. How can I check whether the DB connection has been closed?
niao
Well it depends on your data access layer. Do you use an OR/M or do you manage the SessionScope/TransationScope/DBConnection objects yourself?
Russell
I manage SessionScope/TransationScope/DBConnection objects myself, each select query starts with the following statement using(SqlCommand sqlcmd = new SqlCommand(...)), I also have TransactionScope in "using"
niao
Ok. Do you have a finally clause with the scope.Complete() method called? This will guarantee the transaction is closed. You could also monitor the number of connections to your database: "select db_name(dbid) as db, count(dbid) as connection from sys.sysprocesses where dbid > 0 group by dbid"
Russell
yes I have scope.Complete() clause. The problem occurs when i just use simple SELECT functions that are included in using(SqlCommand sqlcmd = new SqlCommand(query, connection)){}
niao
well, the problem was that I have indeed unclosed sql connections
niao
A: 

You could set up a web farm (multiple processes for the same IIS) which would help minimise the issue, and depend on a single process less (if one dies and restarts, the others can be there to hold up the fort until it restarts.

A: 

As an aside, your code above is equivalent to:

catch (Exception ex)
{
    Factory.Abort();
    return null;
}
finally
{
    Factory.Close();
    Factory.Abort();
}

And it's just as bad. You probably want to log the exception somewhere so that you know what happened.

I'd like to see the code that calls on the database. I'd be concerned you might not be cleaning up properly.

John Saunders
Here is the code that calls on the databaseinternal SqlConnection CheckIfConnectionOpen() { if (_Connection.State != ConnectionState.Open) { _Connection.Open(); } return _Connection; }using (SqlCommand cmd = new SqlCommand(query, _Connection)){//some parameters for sqlcommand here//and execute nonQuery or execute reader}
niao
and actually the catch code does not catch any exception, I just have a null response from the server.
niao