Re: 2 ways to close a connection
From: Angel Saenz-Badillos (angelsbadillos_at_hotmail.com)
Date: 05/04/04
- Next message: Max: "VB.NET - Detecting the attachment of removable storage?"
- Previous message: Sven Groot: "Re: Using a variable to build the name of a variable for assignment"
- Maybe in reply to: Angel Saenz-Badillos: "Re: 2 ways to close a connection"
- Next in thread: Angel Saenz-Badillos: "Re: 2 ways to close a connection"
- Messages sorted by: [ date ] [ thread ]
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
> >
> >
- Next message: Max: "VB.NET - Detecting the attachment of removable storage?"
- Previous message: Sven Groot: "Re: Using a variable to build the name of a variable for assignment"
- Maybe in reply to: Angel Saenz-Badillos: "Re: 2 ways to close a connection"
- Next in thread: Angel Saenz-Badillos: "Re: 2 ways to close a connection"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|