Re: .NET connection pool problems

From: Peter Bromberg [C# MVP] (pbromberg_at_yahoo.com)
Date: 02/21/04


Date: Sat, 21 Feb 2004 10:21:28 -0500

As I recall, the documentation especially from .NET 1.0 on this area was
confusing. In 1.1, both Dispose and Close are acceptably best practices for
closing a connection and returning it to the pool.

Here is a snippet from the Data Access Architecture best practices article
on MSDN along with some sample code. Note that they illustrate the use of
both methods, with Dispose being called automatically for you at the end of
a "using" block:
------------------------------------------------------------------
Connection Usage Patterns
Irrespective of the .NET data provider you use, you must always:

Open a database connection as late as possible.
Use the connection for as short a period as possible.
Close the connection as soon as possible. The connection is not returned to
the pool until it is closed
through either the Close or Dispose method. You should also close a
connection even if you detect that it
has entered the broken state. This ensures that it is returned to the pool
and marked as invalid. The
object pooler periodically scans the pool, looking for objects that have
been marked as invalid.
To guarantee that the connection is closed before a method returns, consider
one of the approaches
illustrated in the two code samples that follow. The first uses a finally
block.
The second uses a C# using statement, which ensures that an object's Dispose
method is called.

The following code ensures that a finally block closes the connection.
Note that this approach works for both Visual Basic .NET and C# because
Visual Basic .NET
supports structured exception handling.

public void DoSomeWork()
{
  SqlConnection conn = new SqlConnection(connectionString);
  SqlCommand cmd = new SqlCommand("CommandProc", conn );
  cmd.CommandType = CommandType.StoredProcedure;

  try
  {
    conn.Open();
    cmd.ExecuteNonQuery();
  }
  catch (Exception e)
  {
    // Handle and log error
  }
  finally
  {
    conn.Close();
  }
}

The following code shows an alternate approach that uses a C# using
statement.
Note that Visual Basic .NET does not provide a using statement or any
equivalent functionality.

public void DoSomeWork()
{
  // using guarantees that Dispose is called on conn, which will
  // close the connection.
  using (SqlConnection conn = new SqlConnection(connectionString))
  {
    SqlCommand cmd = new SqlCommand("CommandProc", conn);
    fcmd.CommandType = CommandType.StoredProcedure;
    conn.Open();
    cmd.ExecuteQuery();
  }
}

You can also apply this approach to other objects-for example, SqlDataReader
or OleDbDataReader-
which must be closed before anything else can be done with the current
connection.
----------------------------------------------------------------

"greg" <gregl@insauctionNOSPAM.com> wrote in message
news:OrRnQ$%239DHA.684@TK2MSFTNGP10.phx.gbl...
> Hi
>
> We have w2k, iis5, .NET/c#
>
> I periodically receive this message and the system freezes
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++
>
> 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.
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> in all data access objects i USE destructor that closes connection:
>
> public SBSite(String siteId){
> String cString =
>
System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
> conn = new SqlConnection(cString);
> conn.Open();
> }
>
> public DO_SOMETHING_USING_CONNECTION() {}
>
> ~SBSite()
> {
> conn.Close();
> }
>
> BUT the problem persists
>
> HELP
> also where connection pooling is enabled/disabled for .NET/c#?
> how can I increase pool size?
>
> THANKS
> GSL
>
>



Relevant Pages

  • Close and Dispose argument
    ... here is an email conversation between me and Microsoft ... connections from the pool is still a debate. ... Programming with Microsoft ADO.NET" module 2, p 18: "Calling the Dispose ... method removes the conneciton from the connection pool". ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: What is the difference between Dispose and Close
    ... Dispose and Close both return the connection to the pool. ... were calling neither or the logic let the PSP sneak away without calling one ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Max pool size reached exception when trying to open a new connection
    ... Hi i am wrapping the sqlconnection in using statement.so there is no ... doubt that dispose is not getting called on sqlconnection. ... prior to obtaining a connection from the pool. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: VS2005 and SQL Express: Internal .Net Framework Data Provider
    ... connection object you use. ... public void CreateConnection{ ... // Close first and dispose the existing data reader is there ... release resources used by the object ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Ideal way to retrive dataset.
    ... In general it is a good idea to call dispose on any method that implements ... The best way to use the SqlConnection object in c# is ... closeand disposeconnection, and dispose SqlDataAdapter also. ... the connection will go back to the pool in this case. ...
    (microsoft.public.dotnet.framework.adonet)

Loading