Re: garbage collection

From: Bijoy Naick (b_naick_at_yahoo.ca)
Date: 12/14/04


Date: Tue, 14 Dec 2004 15:15:23 -0500

Understood.. so what happens in the following case..

Assume that I have a custom class - call it Events. One of the methods of
this class is GetEvents defined/implemented as follows:

public function GetEvents(startDate, endDate) as sqlDataReader
    Dim myReader As SqlDataReader
    Dim oConn As SqlConnection

    oConn.Open()
    sql = "select * from events in date range"
    myReader = oConn.ExecuteReader(sql)
    oConn.Close()
    return myReader
end function

If an aspx page creates an instance of the Events class and calls the
GetEvents method, it will get a reader back.. How can I close the reader
defined in the GetEvents method?

BTW: I took a look at the code in the Application Block provided by Msft
called SqlHelper. Their code doesnt close the reader either..

Bijoy

"Kumar Reddi" <KumarReddi@REMOVETHIS.gmail.com> wrote in message
news:ORFESfh4EHA.4028@TK2MSFTNGP15.phx.gbl...
>
> First of all, with datareaders, if you do not close the reader after the
> usage, you can not reuse the connection on which it opened. This is not a
> garbage collection issue, its a logical error. Garbage collection cleans
the
> resources for you, by doing automatic memory deallocation, but it doesnt
> close the datareader for you. Thats your job. Also, If your application
> doesnt allow database connection pooling, you need to dispose your
database
> connection. But, your application seems to be using the connection
pooling,
> so you do not have to worry. By the way, the reason you need to call
dispose
> on database connection is, it is unmanaged object. GC can not clean the
> unmanaged resources for you
> --
> Kumar Reddi
> http://kumarreddi.blogspot.com
>
> "Bijoy Naick" <b_naick@yahoo.ca> wrote in message
> news:uTRytYh4EHA.1564@TK2MSFTNGP09.phx.gbl...
> > Just wondering if anyone has experienced any issues with garbage
> collection
> > in .net. We developed an application using VB .NET; many of the pages
made
> > db calls.
> >
> > One of the developers forgot to close the connection(s) and DataReader
> > objects. We found that once the site recieved a large number of hits,
the
> > server died on us and memory usage was ridiculously high. Since that,
> we've
> > closed all the connections and datareaders and the app works great..
> >
> > Should the .NET GC not have managed all of this for us?
> >
> > BTW: I know its good coding practice to close everything you open.. but
> just
> > wondering IF GC does this or not..
> >
> > Bijoy
> >
> >
>
>



Relevant Pages

  • Re: garbage collection
    ... You can not return reader after the connection is closed. ... But to return a dataReader you need to keep the connection open. ... Also, If your application>> doesnt allow database connection pooling, you need to dispose your> database ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: closing DataReader in another layer
    ... > CommandBehaviour.CloseConnection makes the reader so that when the reader ... > call Closeon the reader somewhere else, and not leak a connection. ... > Michael Giagnocavo ... not happen correctly on garbage collection of the DataReader. ...
    (microsoft.public.dotnet.languages.csharp)
  • Three Phases To Email Sensitivity
    ... the associationphase, the connection phase, and the ... They should have the right words thatthe reader can transform back ... When writing your response, you willwant to make ... If the topic is about apples, you donot want to add an orange ...
    (comp.lang.vhdl)
  • Re: CommandBehavior.CloseConnection question
    ... > When you close the reader, the connection is closed if you use ... Dim dbReader as SqlDataReader ... Dim ConnectionString as String ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Multi-threaded apps and data readers
    ... RightHand .NET consulting & development www.rthand.com ... Blog: http://cs.rthand.com/blogs/blog_with_righthand/ ... So if I kept the connection open all ... commands reader, how would I prevent the readers from clashing or do ...
    (microsoft.public.dotnet.framework.adonet)