connection pooling error

  • Thread starter Thread starter Chris Szabo
  • Start date Start date
C

Chris Szabo

I've created a data access layer for a .NET web
application. I'm using C# and framework 1.1. I'm getting
an error from time to time when I close a connection
saying: System.InvalidOperationException: A connection
pooling error has occurred.

It doesn't happen each time, the exception is thrown
randomly. The big problem is that the connection is left
active in the pool. After a short period of time, the
pool is filled up and people can't access the site.

Has anyone ever run into this before? Any ideas on how I
might correct the problem? Any help is greatly
appreciated.

Chris Szabo.
 
Sure... here's some code.

private void killConn()
{
if (cmdSQL.Connection != null)
{
try
{
cmdSQL.Connection.Close();
cmdSQL.Connection.Dispose();
}
catch ( Exception e )
{
l.writeException ( e );
}
}
}

public SqlCommand cmdSQL
{
get
{
if ( _cmdSQL == null )
_cmdSQL = new SqlCommand ( );

if ( _cmdSQL.Connection == null )
_cmdSQL.Connection = new SqlConnection(connString);

if ( _cmdSQL.Connection.ConnectionString == null ||
_cmdSQL.Connection.ConnectionString==String.Empty )
_cmdSQL.Connection.ConnectionString = connString;

if ( _cmdSQL.Connection.State ==
ConnectionState.Closed )
{
try
{
_cmdSQL.Connection.Open ( );
}
catch ( Exception e )
{
l.writeException ( e );
}
}
return _cmdSQL;
}
}
 
Hmm... some things to consider..

You could use the "Singleton" pattern and have a static connection that gets
reused because really - you can have multiple operations happen over one
connection. if you do this, everytime before you make a SQL call, make sure
the connection is open, if it's not - then open it.

but the first thing that came to mind is that you are trying to manually
..Dispose() of the object, and for connection pooling - it seems to be all or
nothing. Either you control all connections opening or closing - or you let
cp handle it.. So I'd say get comment out the .Dispose() and see if this
still happens..

And if the database is SQL2K - you can run the SQL Profiler and watch the
connections and see what happens every time there is a new connection..

hth
 
Okay, I'll give that a try. Can you fill me in on
the "Singleton" pattern? I've actually been trying to
accomplish this since we expect massive amounts of traffic
on the site. I haven't been able to come up with anything.

Thanks,
Chris Szabo.
 
Kill cmdSQL.Connection.Dispose(); GC issue, with Dispose() left alive
until blah blah blah maybe?
 
I've modified the code as follows and I'm still getting
the same error. I can actually step through it, verify
that _cmdSQL is not null, .Connection is not null, and the
state is open. When I reach the try catch block an
exception is still thrown, A connection pooling error has
occurred.

private void killConn()
{
if ( _cmdSQL != null )
{
if ( _cmdSQL.Connection != null )
{
if ( _cmdSQL.Connection.State !=
ConnectionState.Closed )
{
try
{
_cmdSQL.Connection.Close();
}
catch ( Exception e )
{
l.writeException ( e );
}
}
}
}
}

Any more ideas...?
 
Back
Top