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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cylix

    #1

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

    Does anyone can explain the details between
    Connection.clos e
    Connection.disp ose
    Connection = nothing

    If I just need to close the connection and release the memory,
    do I need to write all three statement?
    Thank you.

  • Sahil Malik [MVP C#]

    #2
    Re: Please explain the difference between close, dispose, = nothing on sqlClient.

    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





    "Cylix" <cylix2000@gmai l.comwrote in message
    news:1156822977 .390323.55940@m 79g2000cwm.goog legroups.com...
    Does anyone can explain the details between
    Connection.clos e
    Connection.disp ose
    Connection = nothing
    >
    If I just need to close the connection and release the memory,
    do I need to write all three statement?
    Thank you.
    >

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Please explain the difference between close, dispose, = nothing on sqlClient.

      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 connectionstrin g in the
      connection object. The same as
      setting before the close the XXXconnection.c onnectionstring to nothing.
      Dispose is calling Close before that and therefore has for the
      connectionpooli ng the same effect. AFAIK is that connectionstrin g 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

      "Cylix" <cylix2000@gmai l.comschreef in bericht
      news:1156822977 .390323.55940@m 79g2000cwm.goog legroups.com...
      Does anyone can explain the details between
      Connection.clos e
      Connection.disp ose
      Connection = nothing
      >
      If I just need to close the connection and release the memory,
      do I need to write all three statement?
      Thank you.
      >

      Comment

      • Cylix

        #4
        Re: Please explain the difference between close, dispose, = nothing on sqlClient.

        Oh, Thanks Sahil and Cor.
        You do really helpful.

        Comment

        Working...