none
What does Dispose do on an Auto-Generated TableAdapter? RRS feed

  • Question

  • Hiya!

    I have a number of classes each creating a number of auto-generated table adapters and I noticed recently that I was not calling the Dispose method on the auto-generated table adapters.  I am using a SQL server database, and in the auto-generated code I can see the partial class contains an instance of a SqlDataAdapter, a SqlConnection, a SqlCommand[], and a bool indicating whether to clear before fill.

    I am using a shared connection between the table adapters and I handle the Open, Close/Dispose of the SqlConnection already.  I have read a few articles that the SqlConnection is not diposed of in the table adapters dispose method, which I think makes sense as like me you might want to use a shared connection between table adapters, etc. 

    So my question is, is what exactly is the Dispose method doing and is there any reason I would need to call this?  With my current architecture my classes that contain the table adapters simply goes out of scope and cannot be accessed after this... I leave it to the magic of .NET to do any garbage collection on my classes and its table adapters... does this all sound ok or is there something I am missing?

    Thanks in advance for any help on this one... I'm intrigued to find out why the table adapter has a Dispose method and what it does.

    Monday, April 19, 2010 1:32 PM

Answers

  • Hello,

     

    Welcome to ADO.NET DataSet forum!

     

    TableAdapter inherits System.ComponentModel.Component.  Its Dispose method calls its base class’s Dispose method.  Here are the implementation of the Component.Dispose() in .NET Reflector. 

    ==============================================================================

    public void Dispose()

    {

        this.Dispose(true);

        GC.SuppressFinalize(this);

    }

    ==============================================================================

    protected virtual void Dispose(bool disposing)

    {

        if (disposing)

        {

            lock (this)

            {

                if ((this.site != null) && (this.site.Container != null))

                {

                    this.site.Container.Remove(this);

                }

                if (this.events != null)

                {

                    EventHandler handler = (EventHandler) this.events[EventDisposed];

                    if (handler != null)

                    {

                        handler(this, EventArgs.Empty);

                    }

                }

            }

        }

    }

    ==============================================================================

     

    Based on MSDN documents of Compoment.Dispose(Boolean), it releases both the managed and unmanaged resources if the input parameter is true.   

     

    So if the TableAdapters are not global variables which exist the entire lifetime of your application, it is recommended to call .Dispose method if you don’t need it anymore.  Of course, it won’t cause huge problems if we don’t call .Dispose manually, GC will help us to release the managed resource once the TableAdapter is not used again.  .Finalize method of the TableAdapter will help us release the unmanaged resources, http://msdn.microsoft.com/en-us/library/498928w2(v=VS.100).aspx.   

     

    I agree that TableAdapter.Dispose() does not close the inside connection, so the drawback is the connection is still occupied if we don’t call the TableAdapter.Connection.Close() method manually.  But since you are sharing the connection between the TableAdapters, I think it is fine.   Please make sure you control the connection correctly and close it so it will be put back to the connection pool for reuse. 

     

     

    If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 20, 2010 3:28 AM
    Moderator

All replies

  • Hello,

     

    Welcome to ADO.NET DataSet forum!

     

    TableAdapter inherits System.ComponentModel.Component.  Its Dispose method calls its base class’s Dispose method.  Here are the implementation of the Component.Dispose() in .NET Reflector. 

    ==============================================================================

    public void Dispose()

    {

        this.Dispose(true);

        GC.SuppressFinalize(this);

    }

    ==============================================================================

    protected virtual void Dispose(bool disposing)

    {

        if (disposing)

        {

            lock (this)

            {

                if ((this.site != null) && (this.site.Container != null))

                {

                    this.site.Container.Remove(this);

                }

                if (this.events != null)

                {

                    EventHandler handler = (EventHandler) this.events[EventDisposed];

                    if (handler != null)

                    {

                        handler(this, EventArgs.Empty);

                    }

                }

            }

        }

    }

    ==============================================================================

     

    Based on MSDN documents of Compoment.Dispose(Boolean), it releases both the managed and unmanaged resources if the input parameter is true.   

     

    So if the TableAdapters are not global variables which exist the entire lifetime of your application, it is recommended to call .Dispose method if you don’t need it anymore.  Of course, it won’t cause huge problems if we don’t call .Dispose manually, GC will help us to release the managed resource once the TableAdapter is not used again.  .Finalize method of the TableAdapter will help us release the unmanaged resources, http://msdn.microsoft.com/en-us/library/498928w2(v=VS.100).aspx.   

     

    I agree that TableAdapter.Dispose() does not close the inside connection, so the drawback is the connection is still occupied if we don’t call the TableAdapter.Connection.Close() method manually.  But since you are sharing the connection between the TableAdapters, I think it is fine.   Please make sure you control the connection correctly and close it so it will be put back to the connection pool for reuse. 

     

     

    If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 20, 2010 3:28 AM
    Moderator
  • Thanks very much for your reply, which was very interesting and the link for cleaning up unmanaged resources was very useful!

    However, what I was wondering is what managed and unmanaged resources need releasing in the auto-generated table adapter?

    I can't think of any resources that would need releasing, which is why I wondered whether I actually needed to call Dispose.  When leaving it to the garbage collector if there are no resources to release then the overhead in not calling Dispose should be minimal.

    However, as you rightly say it is recommended to call Dispose when you have finished with the object, and after reading up on those links you posted, I am now thinking I shouldn't care what the Dispose method does internally and should be calling it regardless.

    Thanks again for your help!!

    Tuesday, April 20, 2010 1:18 PM
  • Oh, I agree.  There are not many resources to release of the auto-generated TableAdapter.  J  

     

    Good day!  

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, April 22, 2010 8:29 AM
    Moderator