Proper removal of DbConnection

I have a class called DatabaseHelper that wraps DbConnection. What is the correct way to set this class to use an operator? I implemented IDisposible, but I'm not sure when and where should I call Connection.Close () or Connection.Dispose ().

When I just call Connection.Dispose () in my own Dispose () method, sometimes I get a SocketException from my DbConnection object. I suppose this is because the old connections remain open, but no details are tied to the exception, so I don’t know for sure.

+2
source share
4 answers

. () dispose. IDisposable, IDisposable ..:

public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
    if (!disposed)
    {
        if (disposing)
        {
            // Dispose managed resources.
        }

        // There are no unmanaged resources to release, but
        // if we add them, they need to be released here.
    }
    disposed = true;

    // If it is available, make the call to the
    // base class Dispose(Boolean) method
    base.Dispose(disposing);
}

( http://msdn.microsoft.com/en-us/library/system.idisposable.aspx).

+6

:

IDbConnection.Dispose() ( Reflector):

SqlClient:

protected override void Dispose(bool disposing)
{
       if (disposing)
       {
             switch (this._objectState)
             {
                   case ConnectionState.Open:
                   {
                         this.Close();
                         break;
                   }
             }
             this._constr = null;
       }
       base.Dispose(disposing);
}

Odbc:
protected override void Dispose(bool disposing)
{
       if (disposing)
       {
             this._constr = null;
             this.Close();
             CNativeBuffer buffer1 = this._buffer;
             if (buffer1 != null)
             {
                   buffer1.Dispose();
                   this._buffer = null;
             }
       }
       base.Dispose(disposing);
}

OleDb:
protected override void Dispose(bool disposing)
{
       if (disposing)
       {
             if (this.objectState != 0)
             {
                   this.DisposeManaged();
                   if (base.DesignMode)
                   {
                         OleDbConnection.ReleaseObjectPool();
                   }
                   this.OnStateChange(ConnectionState.Open, ConnectionState.Closed);
             }
             if (this.propertyIDSet != null)
             {
                   this.propertyIDSet.Dispose();
                   this.propertyIDSet = null;
             }
             this._constr = null;
       }
       base.Dispose(disposing);
}

dispose , .

+2

. Dispose (false) .

    #region IDisposable Members
    private bool _isDisposed;

    private void ThrowIfDisposed()
    {
        if (_isDisposed)
            throw new ObjectDisposedException(this.GetType().Name);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!_isDisposed)
        {
            if (disposing)
            {
                //part 1 : disposing managed objects
                _command.Dispose();
                _command.Connection.Dispose();
                if (_command.Transaction != null)
                    _command.Transaction.Dispose();
            }
            //part 2: disposing unmanged objects. Here there are no unmanged objects.
            _isDisposed = true;
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    //~DbCommandExecutor() //No need of finalize here. Because there is no unmanged objects in my class. ie, no code in part 2.
    //{
    //    Dispose(false);
    //}
    #endregion

( ) , 2. . .. , .

compare examples: from msdn

http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx and http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx

+1
source

To complete the IDisposable implementation template, it includes a finalizer (destructor) for your class that calls the Dispose () method (passes false). This acts as a fail-safe mechanism to get rid of unmanaged objects if the class consumer has not called Dispose ().

    ~MyClass() 
    {
        Dispose(false);
    }
0
source

All Articles