What resources are used when querying the database and why do they need to be disposed of?

When accessing the database correctly, C#

I often see this:

Make sure you have selected Reader, Command and Connection object

If I look at the SqlConnection , SqlCommand and SqlDataReader documentation , all I see as a description for the method Dispose

is

Releases all resources used by the Component.

I want to know what resources are used by these three types mentioned above. I thought that only recycling SqlConnection

will have to close and the other two sub-object ( SqlCommand

and SqlDataReader

).

In addition, there are many MSDN examples that do not delete an object SqlCommand

. See this example

So, I have two main problems:

  • What are the exact resources used for each type?
  • What are the consequences of mismanaging each of them?

I know that the database connection is unmanaged code and is not properly garbage collected. I would like to know which object or resources are used by each object and what will happen exactly if I do not dispose of each of them.

So, PLEASE don't answer anything general like unmanaged code, garbage collector, free resources ...

+3


source to share


2 answers


  • This could change. The difference is that they are unmanaged (think c instead of C #) and so we basically just inform the unmanaged component that it should release ANY resources it has used ... This includes closing open connections, freeing memory (since unmanaged the code does not get the benefits of the garbage collector) covering the handles requested from the operating system.

  • Mostly a memory leak. Half open connections on the database side. Failed code reviews and jibes from more experienced developers: P



Try reading: https://msdn.microsoft.com/en-us/library/498928w2(v=vs.110).aspx

+3


source


It's pretty easy to check the source code nowadays. All the necessary information is there. For example, SqlConnection does the following:

  override protected void Dispose(bool disposing) {
        if (disposing) { // release mananged objects

            // V1.0, V1.1 did not reset the Connection, Parameters, CommandText, WebData 100524
            //_parameters = null;
            //_activeConnection = null;
            //_statistics = null;
            //CommandText = null;
            _cachedMetaData = null;
        }
        // release unmanaged objects
        base.Dispose(disposing);
    }

      

base.Dispose does the following:



  protected virtual void Dispose(bool disposing) {
        if (disposing) {
            lock(this) {
                if (site != null && site.Container != null) {
                    site.Container.Remove(this);
                }
                if (events != null) {
                    EventHandler handler = (EventHandler)events[EventDisposed];
                    if (handler != null) handler(this, EventArgs.Empty);
                }
            }
        }
    }

      

This is how metadata and event handlers are cached, among other things.

If the class implements IDisposable

, then there is a very good reason and you should probably use a block using

.

+1


source







All Articles