Please explain the difference between close, dispose, = nothing on sqlClient.

  • Thread starter Thread starter Cylix
  • Start date Start date
C

Cylix

Does anyone can explain the details between
Connection.close
Connection.dispose
Connection = nothing

If I just need to close the connection and release the memory,
do I need to write all three statement?
Thank you.
 
Close - closes the Connex and makes it available for connex pooling
Displose - closes the Connex and makes it available for connex pooling and
clears stateful information
Connection = nothing will leave the connex for garbage collection. If an
open connex was set to nothing, then you are at the mercy of GC to come call
the finalizer i.e. your connex is going to be unpooled for a fairly long
time.

In short.

#1 - Dispose is the best
#2 - Close is almost as good
#3 - Connection = nothing is horrible.

Did that help ya? :)

- Sahil Malik
http://www.winsmarts.com
http://blah.winsmarts.com
 
Cylix,

Close makes it possible for the SQLServer to remove the connection from the
connection pooling.

Dispose is removing as extra the reference to the connectionstring in the
connection object. The same as
setting before the close the XXXconnection.connectionstring to nothing.
Dispose is calling Close before that and therefore has for the
connectionpooling the same effect. AFAIK is that connectionstring the only
reference that can prevent it from being garbaged and therefore this will be
done.

Setting it to nothing has sense if you have set your connection globally
because than the reference to that will be cleaned and the last one can be
garbaged, in other cases it is without sense. Declaring it globaly is by
instance the way as versions 2002/2003 do it using the wizards. I would try
to prevent that.

I hope this gives an idea,

Cor
 
Back
Top