Re: 2 ways to close a connection

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Angel Saenz-Badillos (angelsbadillos_at_hotmail.com)
Date: 05/04/04


Date: 4 May 2004 16:06:26 -0700

Marina,
Great to see you and Cor again talking about pooling, you guys do a
great job in this newsgroup, thank you!

As far as the code below, the discussion was not whether to use close
or dispose, the issue that the code snippet I posted addresses was
whether to rely on GC to close the connections for you.

Try the repro code below after removing the following line:
conn.Close()

and you should see a much larger number of connections being created
as the GC cannot keep up with con.open.

The code that you have below is correct _as long as_ ExecuteReader
does not throw an exception. Every time that execute throws an
exception close will not be called and you will end up with the
behavior shown above. You can try this by adding a throw command in
your code after execute.

Bottom line:
The code for Dispose does two things:
1) sets the connection string to ""
2) calls close.
There is no difference between calling either close or dispose (or
both), but you have to make sure to call them during a "finally" to
ensure that you close the connection even when there is an exception.
We added IDisposable support for ado.net classes just so that we could
use the "using" clr construct which is the most visually pleasing way
to write this type of code. In VB.NET you have to make sure to use try
finally blocks.

-- 
Angel Saenz-Badillos [MS] Managed Providers
This posting is provided "AS IS", with no warranties, and confers no 
rights.
Please do not send email directly to this alias. This alias is for 
newsgroup
purposes only.
"Marina" <someone@nospam.com> wrote in message news:<#$jhuLYIEHA.2312@TK2MSFTNGP10.phx.gbl>...
> Yes, I guess I was, and I had the same advice then to.
> 
> I just tried reproducing what Angel did. Here is my code:
> 
> Sub Main()
> Try
>   For i As Integer = 1 To 1000
>     ConnTest()
>   Next
>   Console.WriteLine("ok")
> Catch ex As System.Exception
>   Console.WriteLine(ex.ToString())
> End Try
>   Console.ReadLine()
> End Sub
> 
> Private Sub ConnTest()
>   Dim conn As New SqlConnection(connString)
>   Dim command As New SqlCommand("Select count(*) from mytable", conn)
>   conn.Open()
>   command.ExecuteReader().Read()
>   conn.Close()
> End Sub
> 
> Doing this, I would run it in debug mode, and pause it, at say i =  100,
> etc.  At all times, there was never more then 2 connections from this app on
> my db server (I passed in an 'applicationname' parameter in the connection
> string, to make sure I knew which connections were coming from my test app).
> 
> So as of yet, I have not been convinced, as the example that was given, does
> not produce the results that it supposedly would.
> 
> "Cor Ligthert" <notfirstname@planet.nl> wrote in message
> news:uM5Qj6XIEHA.1944@TK2MSFTNGP11.phx.gbl...
> > Hi Marina,
> >
> > I thought that you where also in Angel Saenz-Badillos made some sample
>  code
> > to try it, however he did sound so sure (and I never saw him write strange
> > things) that I absolute did found it not needed to test it.
> >
> > http://tinyurl.com/2sh6k
> >
> > This is the thread. (and I saw you where in it too) :-)
> >
> > Cor
> >
> >


Relevant Pages

  • Re: FieldInfo Class - SetValue Member Method
    ... Cor, I've found the solution from Jay's previous replies to the newsgroup, ... Public Structure testing ... Private Sub starttest() ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Convert C# to vb.net help
    ... Convert it first to argb that goes forerver, a piece of code Peter Huang ... showed recently in this newsgroup. ... End Sub ... Cor ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Convert C# to vb.net help
    ... Convert it first to argb that goes forerver, a piece of code Peter Huang ... showed recently in this newsgroup. ... End Sub ... Cor ...
    (microsoft.public.dotnet.framework)
  • Re: DataTable and AutoIncrement
    ... Thanks Cor. ... You help a lot of people on this newsgroup. ... "Cor Ligthert" wrote: ... > End Sub ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Sql connection opening two connections
    ... I would ask this question in the newsgroup ... On friday is Angel sometimes active in that newsgroup, he is often busy with ... connections. ... Cor ...
    (microsoft.public.dotnet.languages.vb)