locked
Unmanaged Resouce SQLConnection RRS feed

  • Question

  • Hi All

    Can someone tell me why the ADO SQLConnection Class is a Unmanaged Resource? and I have to explicitly Dispose it?

    What I don't understand is that the SQLConnection Class is a .NET class and therefor the GC should know how to free the Objects memory form the Managed Heap right?

    Thanks

    Wednesday, July 16, 2014 9:48 AM

Answers

  • Hi,

    Since ADO.NET 2.0 the SqlClient - including SqlConnection - is fully managed code.

    But a SqlConnection uses resources like network TCP sockets or IPC. So some underlying components aren't managed as they are using operating system resources.

    Note: Dispose will also call Close and the connection is released to the connection pool

    Regards, Elmar

    Wednesday, July 16, 2014 12:39 PM

All replies

  • Hello,

    Keeping in mind best practice for connections is create, use, dispose which can be done via a using statement and a using statement can be used for a command object too. If a connection needs to live and breath for the life time of the app you could use a shared instance of a connection thus no need to be concern about over usage.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Wednesday, July 16, 2014 10:52 AM
  • Hi

    Thanks for your response, but im trying more to find out why SQLConnection is a unmanaged resource in the first place? I mean if the SQLConnection is a .NET ADO Class and written in .NET, then why would it be a Unmanaged resource?

    Thanks

    Wednesday, July 16, 2014 11:01 AM
  • Hi,

    Since ADO.NET 2.0 the SqlClient - including SqlConnection - is fully managed code.

    But a SqlConnection uses resources like network TCP sockets or IPC. So some underlying components aren't managed as they are using operating system resources.

    Note: Dispose will also call Close and the connection is released to the connection pool

    Regards, Elmar

    Wednesday, July 16, 2014 12:39 PM
  • In addition to Elmar, 

    Close calls Dispose 

    :-)

    In version 1.x the DBConnection was the dispose method overriden and was the connectionstring removed from the object. 


    Success
    Cor

    Wednesday, July 16, 2014 1:06 PM