Dispose connection?

snow1

Member
Joined
Apr 21, 2006
Messages
17
Programming Experience
Beginner
Hi! Sorry if i don't make sense here, but i am really puzzled. Can anyone explain this simple concept to me? When do i Dispose connection? is it necessary? and there's also this blablabla = Nothing. how do i use it? And how about the DataAdapter, do i dispose it also?
 
Last edited:
An object's Dispose method ostensibly destroys the object. It releases all the resources associated with that object, so you should call the Dispose method of every object that has one when you're finished with it. Note that any objects created in the designer will be Disposed when the form is Closed. Setting a variable does exactly what it implies: afterwards the variable refers to no object. Doing so is generally pointless because the vast majority of variables will lose scope soon afterwards and cease to exist anyway. The only time it is worthwhile setting a variable to Nothing is when the variable will not, or may not, lose scope for some time after you have no further use for it, like if the variable is a field (class-level variable) or a local variable in a long-running block of code. When you set a variable to Nothing, or a variable loses scope, that reference to the object is removed. When an object has no remaining references it is available for garbage collection, so the space it occupies in memory can be reclaimed. If you don't Dispose objects that support it then it will take at least two passes of the garbage collector to clean them up: one to Finalize the object (call the Finalize method, which implicitly calls the Dispose method) and another to reclaim its memory.

In short:

1. If an object has a Dispose method and you have no further use for that object then call the method.
2. If a variable refers to an object that you have no further use for, Disposed or not, and that variable will not or may not lose scope for some time then set that variable to Nothing.

Obviously if you have no further use for an object with a Dispose method and the variable that refers to it will not lose scope for some time you need to perform both these operations. In that case the Disposal must come first of course.
 
Incidentally.. I never heard microsoft saying to dispose connections.. only close them. There is an interim mechanism called a connection pool, it operates transparently and you dont have any interaction with it (apart from getting a conenction from the pool with Open, and returning it to the pool with Close) and it is responsible for managing the open connections..

So to answer your question on disposing connections - dont. Just close them. I would expect that if DIspose was a problem, microsoft would have overridden it to be useless anyway(and provided dispose methods internally under other names)
 
cjard said:
if DIspose was a problem, microsoft would have overridden it to be useless anyway

Never ever expect tooooo much from Microsoft. The M$ team is able to surprise you even when you think you know everything about their plans and thoughts. They have surprise method called "sudden silences" ... lol hahahahahahahahaha :D
 
A database connection object is a component, i.e. it inherits from the Component class, which implements the IDisposable interface. The SqlConnection class explicitly overrides the Dispose method to provide its own implementation:
VB.NET:
[COLOR=#1000a0]Protected[/COLOR] [COLOR=#1000a0]Overrides[/COLOR] [COLOR=#1000a0]Sub[/COLOR] [B]Dispose[/B]([COLOR=#1000a0]ByVal[/COLOR] [B]disposing[/B][COLOR=#1000a0] As [/COLOR][URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=1"]Boolean[/URL])
      [COLOR=#1000a0]If[/COLOR] [URL="http://www.vbdotnetforums.com/"]disposing[/URL] [COLOR=#1000a0]Then[/COLOR]
            [COLOR=#1000a0]Me[/COLOR].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=2"]_userConnectionOptions[/URL] = [COLOR=#800000]Nothing[/COLOR]
            [COLOR=#1000a0]Me[/COLOR].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=3"]_poolGroup[/URL] = [COLOR=#800000]Nothing[/COLOR]
            [COLOR=#1000a0]Me[/COLOR].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=4"]Close[/URL]
      [COLOR=#1000a0]End If[/COLOR]
      [COLOR=#1000a0]Me[/COLOR].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=5"]DisposeMe[/URL]([URL="http://www.vbdotnetforums.com/"]disposing[/URL])
      [COLOR=#1000a0]MyBase[/COLOR].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=6"]Dispose[/URL]([URL="http://www.vbdotnetforums.com/"]disposing[/URL])
[COLOR=#1000a0]End Sub[/COLOR]
You should dispose EVERY object that supports it after you're finished with it. If you create a connection object in the designer then this is taken care of for you when the form is disposed. If you create a connection object in code then it is your responsibility to Dispose it. Microsoft doesn't have to explicitly say to dispose connections because it is implicit in the fact that it has a Dispose method that you do it.
 
Back
Top