none
exception while SQLConnection.Open Attempted to read or write protected memory. This is often an indication that other memory is corrupt. RRS feed

  • Question

  • In my application I am using multiple threads for different sql transactions each thread is creating a new SQLConnection object, fetching data and after that closing connection. after some time I am facing following exception from while opening connection.

    Following is the stack trace for this

    <!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman";} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; font-size:10.0pt; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->

    Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

                    Source=System.Data

                    StackTrace=   at SNIWriteSync(SNI_Conn* , SNI_Packet* , SNI_ProvInfo* )

       at SNINativeMethodWrapper.SNIWriteSync(SafeHandle pConn, SafeHandle packet)

       at System.Data.SqlClient.TdsParserStateObject.WriteSni()

       at System.Data.SqlClient.TdsParserStateObject.WritePacket(Byte flushMode)

       at System.Data.SqlClient.TdsParser.TdsLogin(SqlLogin rec)

       at System.Data.SqlClient.SqlInternalConnectionTds.Login(Int64 timerExpire, String newPassword)

       at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)

       at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)

       at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)

       at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)

       at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)

       at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)

       at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)

       at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)

       at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)

       at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)

       at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)

       at System.Data.SqlClient.SqlConnection.Open()

    Tuesday, July 13, 2010 6:41 PM

All replies

  • Do you use database transactions? Do you use those transactions to only to fetch data or to modify data in database as well? Try to run SQL Profiler tool that comes with SQL Server to see if connections are really closed. Also try to disable connection pooling to see if that solves the issue.
    Val Mazur (MVP) http://www.xporttools.net
    Wednesday, July 14, 2010 10:32 AM
    Moderator
  • No I am not using any transactions

     


    1. Open Connection if it is closed
    2. Fetch Data
    3. Close connection

    ConnectionTimeOut is 0 for our all connection and that is maximum.


    following is the code from our application

    protected   virtual bool            Connect()
         {
           
            try
            {
               if (this.sqlConnection.State==ConnectionState.Open)
                  return true;

               this.autoDisconnect = Convert.ToBoolean(this.properties["AUTODISCONNECT"].PropertyValue);
               this.sqlConnection.ConnectionString = GetConnectionString();
               this.sqlConnection.Open();
               return true;
            }
          }

         protected   virtual void            Disconnect()
         {
           
             try
            {
             
               if (this.sqlConnection.State==ConnectionState.Open)
                  this.sqlConnection.Close();
            }
           }


    public override bool Execute()
         {
            try
            {
               this.sqlCommand.Connection      = this.sqlConnection;
               this.sqlCommand.CommandText     = this.commandText;
               this.sqlCommand.CommandType     = CommandType.StoredProcedure;
               this.sqlCommand.CommandTimeout  = Convert.ToInt32(this.properties["COMMANDTIMEOUT"].PropertyValue);

               this.dataSet = new DataSet();

           
               for (int index=0; index<this.retry; index++)
               {
                  try
                  {
                         //Call Connect method to make sure that connection is made
                         Connect();
                         bool returnCode = ExecuteQuery();
                         return dalReturnCode;
                  }
                  catch (SqlException ex)
                  {
                     if (index==this.retry-1 || (ex.Number!=17 && !ErrorHandling.CheckRetriableError(ex)))
                        throw(ex);

                     if (ex.Number == 17)
                     {

                        Disconnect();
                        if (Convert.ToBoolean(this.dataSource.ConnectionProperties["STATUS"].PropertyValue))
                        {
                      
                           this.dataSource.ConnectionProperties["STATUS"].PropertyValue = false;
                        }
                        else
                        {
                        
                           throw(ex);
                        }
                     }

                    
                  }
               }
               return false;  // It should never get here.
            }
            catch (SqlException ex)
            {
               TraceEx.WriteSQLException(ex);
               throw(ex);
            }
            catch (Exception ex)
            {
               throw(ex);
            }
            finally
            {
               if (this.autoDisconnect)
                  Disconnect();

           
            }
         }


    We are not calling dispose method as we are not using any event for SQLConnection.

    and I suppose SQLConnection.Dispose will not do anything in this case

    following is the code I can see from reflector

      protected override void Dispose(bool disposing) {
        if (disposing)
    {        
    this._userConnectionOptions = null;
    this._poolGroup = null;         this.Close();
    }
    this.DisposeMe(disposing);    
    base.Dispose(disposing);
    }    
    Friday, July 23, 2010 6:00 PM
  • Did you run SQL Profiler to see if you have connetions left opened? I am suspecting that database connections are not closed properly from the code. Did you also try to disable connection pool?
    Val Mazur (MVP) http://www.xporttools.net
    Saturday, July 24, 2010 10:20 AM
    Moderator
  • Hi Val,

    Thanks for suggestion, I have to try your solution we had seen this issue in customer environment, so have to go through a process to try that :(. anyhow the possibility of connection open is very rare as we are closing connection after each transaction and it is really difficult to get profiler trace as we are transacting from multiple databases at a single time by multiple threads.

    I will run try by disabling connection pool also.

     

    One more thing in some of the modules we are using old ODBC (ADO) connection for our transactions and the problem is starting from there. Is there any possibility for getting problem from there?

     

    Saturday, July 24, 2010 5:59 PM
  • Are you using OBDC .NET Managed Provider for that or old COM-cased ADO API? If it is ADO API, then you might start to have issues if you do not properly dispose COM objects, since Garbage Collector does not know how to collect unmanaged objects if they are not marked for collection explicitly. If I am not mistaken, you need to use ReleaseComObject method of Marshall class to do it. Here is more description about how to use this method

    http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx

    But my personal recommendation is to avoid using ADO inside of >NET application and use ADO.NET instead

     


    Val Mazur (MVP) http://www.xporttools.net
    Monday, July 26, 2010 10:33 AM
    Moderator