Connection Pooling Problem?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Bob (anonymous_at_discussions.microsoft.com)
Date: 05/19/04


Date: Tue, 18 May 2004 18:26:03 -0700

I have an ASP.NET web application that has been running without any problems for a while. I recently transferred the site to shared hosting and had multiple users start to use the site. The problem I'm experiencing is that when many users are hitting the site at once, occasionaly I will see errors. The 3 most common ones are: "The SqlCommand is currently busy Open, Fetching. ", "Internal Connection Fatal Error", "object reference not set to an instance of an object".

In addition, occassionally the application will lockup for hours after one of these errors. I can navigate to pages, but I cannot log in to my application - I get a "request timed out" error. It usually seems to go away after a few hours - but obviosuly this is very weird behavior.

The research I have done so far indicates that the "The SqlCommand is currently busy Open, Fetching." error usually occurs because a connection that was not properly closed is returned to the connection pool or is accessed when it is still outside the pool (and open) because multiple users are hitting it at the same time. The article I've seen about this (http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20942137.html) mentioned someone putting his data access components in a module, and the accepted solution was to put everything in a class.

However, the Data Access Layer design I'm using has worked in several other multi-user web applications, so it is odd that this is happening. I am using InProcess session state, and other than that I don't know what other configuration setting might be affecting this. Could this be an IIS or hardware configuration problem? It is impossible to replicate this in debug mode because it only happens when multiple users hit the system. Anyway, thanks in advance for the help.

Here is one of the methods where the "SqlCommand is currently busy Open, Fetching" error occurred:

public void GetAllRestaurantsByGroup(RestaurantCollection col, int groupid)
                {
                        try
                        {
                                Connect();

                                PrepareCommand("Restaurant_Select_All_By_Group");
                                m_dsoCommand.AddParameters("@group_id", groupid);
                                                                return m_dsoCommand.ExecuteReader();
                        
        
                                while (m_sqlReader.Read())
                                {
                                        Restaurant oRestaurant = new Restaurant();
                                        oRestaurant.RestaurantName = (string)m_sqlReader["restaurantname"];
                                        oRestaurant.GroupID = (int)m_sqlReader["groupid"];
                                        oRestaurant.LastVisit = (System.DateTime)m_sqlReader["lastvisit"];
                                        oRestaurant.Description = (string)m_sqlReader["description"];
                                        oRestaurant.Distance = (double)m_sqlReader["distance"];
                                        oRestaurant.MapURL = (string)m_sqlReader["MapURL"];
                                        oRestaurant.DBBacked = true;
                                        col.Add(oRestaurant);
                                }
                        }
                        catch(Exception e)
                        {
                                throw e;
                        }
                        finally
                        {
                                Disconnect();
                        }
                }

The code called by this method is below:

                                protected DSOConnection m_dsoConnection;
                                protected SqlDataReader m_sqlReader;

                public void Connect()
                {
                        try
                        {
                                m_dsoConnection.Open();

                        }
                        catch(Exception e)
                        {
                                throw e;
                        }
                }

                                public void Disconnect()
                {
                        try
                        {
                                if (m_sqlReader != null && !m_sqlReader.IsClosed)
                                                                {
                                        m_sqlReader.Close();
                                                                                m_dsoConnection.Close();
                                                                }
                        }
                        catch(SqlException e)
                        {
                                throw e;
                        }
                }

                protected virtual void PrepareCommand(string sStoredProcedure)
                {
                        try
                        {
                                m_dsoCommand.CommandText = sStoredProcedure;
                                m_dsoCommand.Connection = m_dsoConnection.Connection;
                                m_dsoCommand.CommandType = CommandType.StoredProcedure;
                                m_dsoCommand.ClearParameters();
                        }
                        catch(Exception exp)
                        {
                                throw exp;
                        }
                }

Finally, my custom DSOConnection and DSOCommand classes are defined below:

public class DSOConnection
        {
                #region fields

                private string m_connectionString = null;
                private SqlConnection m_connection = null;
        
                #endregion

                #region properties
                public SqlConnection Connection
                {
                        get
                        {
                                return m_connection;
                        }
                }

                #endregion

                #region methods
                #region construction/destruction
                public DSOConnection(string connection)
                {
                        m_connectionString = connection;
                        m_connection = new SqlConnection(m_connectionString);
                }

                #endregion

                #region open/close
                public void Open()
                {
                        try
                        {
                                if(m_connection.State!=ConnectionState.Open)
                                {
                                        m_connection.Open();
                                }
                        }
                        catch(Exception e)
                        {
                                throw e;
                        }
                }

                public void Close()
                {
                        try
                        {
                                if(m_connection.State!=ConnectionState.Closed)
                                {
                                        m_connection.Close();
                                }
                                
                        }
                        catch(SqlException e)
                        {
                                throw e;
                        }
                }
                #endregion

                #endregion
        }

public class DSOCommand
        {
                #region fields
        
                private SqlCommand m_sqlcommand = null;

                #endregion

                #region constructors

                public DSOCommand()
                {
                        m_sqlcommand = new SqlCommand();
                }//End of DSOCommand constructor
                #endregion

                #region Properties

                public int CommandTimeOut
                {
                        get
                        {
                                return m_sqlcommand.CommandTimeout;
                        }
                        set
                        {
                                m_sqlcommand.CommandTimeout = value;
                        }
                }

                public string CommandText
                {
                        get
                        {
                                return m_sqlcommand.CommandText;
                        }
                        set
                        {
                                m_sqlcommand.CommandText = value;
                        }
                }
                public SqlConnection Connection
                {
                        get
                        {
                                return m_sqlcommand.Connection;
                        }
                        set
                        {
                                m_sqlcommand.Connection = value;
                        }
                }

                public CommandType CommandType
                {
                        get
                        {
                                return m_sqlcommand.CommandType;
                        }
                        set
                        {
                                m_sqlcommand.CommandType = value;
                        }
                }
                
                public SqlParameterCollection Parameters
                {
                        get
                        {
                                return m_sqlcommand.Parameters;
                        }
                }//End of CommandType

                #endregion

                #region methods

                                public void ClearParameters()
                {
                        m_sqlcommand.Parameters.Clear();
                }//End of ClearParameters

                public void AddParameters(string sParameterName, object oValue)
                {
                        if(oValue != null)
                        {
                                m_sqlcommand.Parameters.Add(sParameterName,oValue);
                        }
                        else
                        {
                                m_sqlcommand.Parameters.Add(sParameterName,DBNull.Value);
                        }
                }//End of AddParameters

                public void AddParameters(string sParameterName, object oValue, ParameterDirection pdir)
                {
                        SqlParameter p = new SqlParameter(sParameterName, oValue);
                        p.Direction = pdir;

                        if(oValue != null)
                        {
                                m_sqlcommand.Parameters.Add(p);
                        }
                        else
                        {
                                p.Value = DBNull.Value;
                                m_sqlcommand.Parameters.Add(p);
                        }

                }//End of AddParameters

                public SqlDataReader ExecuteReader()
                {
                        return m_sqlcommand.ExecuteReader();
                }//End of ExecuteReader

                public int ExecuteNonQuery()
                {
                        return m_sqlcommand.ExecuteNonQuery();
                }//End of ExecuteNonQuery

                #endregion
        }

I have also included the full text of the error below:

Server Error in '/' Application.

--------------------------------------------------------------------------------

The SqlCommand is currently busy Open, Fetching.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The SqlCommand is currently busy Open, Fetching.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

 
[InvalidOperationException: The SqlCommand is currently busy Open, Fetching.]
   LunchinatorBusinessLayer.Data.Manager.RestaurantManagerDSO.GetAllRestaurantsByGroup(RestaurantCollection col, Int32 groupid) in C:\Documents and Settings\Bob\My Documents\Visual Studio Projects\Lunchinator\LunchinatorBusinessLayer\Data\Manager\RestaurantManagerDSO.cs:266
   LunchinatorBusinessLayer.Business.Collection.RestaurantCollection.GetAllRestaurantsByGroup(Int32 groupid) in C:\Documents and Settings\Bob\My Documents\Visual Studio Projects\Lunchinator\LunchinatorBusinessLayer\Business\Collection\RestaurantCollection.cs:67
   Lunchinator.Main.Results.SetMostVotes() in c:\inetpub\wwwroot\Lunchinator\Main\Results.aspx.cs:92
   Lunchinator.Main.Results.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\Lunchinator\Main\Results.aspx.cs:68
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +731
 
--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573



Relevant Pages

  • RE: Timeout expired. The timeout period elapsed prior to completion of the operation or the server
    ... I noticed the exception was raised by SqlCommand.ExecuteNonQuerymethod. ... Stack Trace: ... The default value of SqlCommand is 30. ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Timeout expired. The timeout period elapsed prior to completion of the operation or the server
    ... Ther are two types of timeout exception that could be raised by SqlClient objects: ... SqlCommand's Timeout decides how long time a command is given for the application that uses the SQlCommand to wait for SQL Server to finish the operation invoced by the SqlCommand. ... System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: SqlCeConnection
    ... error but the Exception object does not contain a message to display. ... private void CloseConnection() ... SqlCeCommand sqlCommand = null; ...
    (microsoft.public.sqlserver.ce)
  • RE: DataReader problem
    ... reader it never changes to false when you reach the end of the reader hence ... SqlCommand com = new SqlCommand; ... any attempt to read data results in the exception ... > DataReader, the _dataReady is false and HasRows is true. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Really need some help on this
    ... Dim oCmd As SqlCommand ... Catch sqlEx As Exception ...
    (microsoft.public.dotnet.framework.aspnet)