Re: Close Reader or not?

Tech-Archive recommends: Speed Up your PC by fixing your registry



Sounds like the service shoud use a thread from the thread pool for the
database work and go on with it's timer in the main service thread.

"tshad" <tfs@xxxxxxxxxxxxxx> wrote in message
news:es5LuXW0GHA.2636@xxxxxxxxxxxxxxxxxxxxxxx
"Scott M." <s-mar@xxxxxxxxxxxxx> wrote in message
news:eQ7$NkU0GHA.4580@xxxxxxxxxxxxxxxxxxxxxxx
ADO.NET .Close() method calls do not throw execptions (any ADO.NET object
that supports a Close() method) if you try to close them and they are
already closed. So, it would never hurt you to call close, even if the
object may already be closed. As such, I always put an additional
Close()
call in the Finally section of my Try...Catch statements since the
Finally
is guaranteed to run, no matter whether an exception was thrown or not.

Would this be for both the Reader and the Connection?

My problem is that I am running this from a Windows Service with a timer
that executes every 10 seconds. So in affect I have the following (using
my
previous example):

*****************************************
Sub Timer_event() (every 10 seconds)
Call Poller()
End Sub

Sub Poller()
Try
Open a Connection
ExecuteReader
Close Reader
Catch

may not happen - Possible if this a Timer in a Windows Service
Finally
Close Reader (or would closing the connection also close
the Reader?)
Close Connection
End Try
End Sub
*******************************************

What happens is if the Sql Server is down, I may be sitting at
ExecuteReader
(or Open a Connection) for longer that 10 seconds.

Then what seems to happen is that the Timer_event() takes over again and
the
Poller never finishes (or at least not that you can see). Poller is
called
again and the same thing can happen.

I am not sure what happens if Poller is called many times without
finishing.

But if I am sitting at ExecuteReader and Poller takes over, I may get a
message at ExecuteReader that says:

"There is already an open DataReader associated with this Connection
which
must be closed first"

I may not even get to the Finally Clause in this case. So I was thinking
of
putting an extra Close Reader and Close Connection in the Timer_Event
before
calling the Poller or maybe in the Try section before the Open Connection.

Does that seem reasonable?

I shouldn't call Connection.State (don't know if the reader has a State
associated with it)?

Thanks,

Tom


"tshad" <tscheiderich@xxxxxxxxxxxxxxx> wrote in message
news:%23fM7aXU0GHA.1292@xxxxxxxxxxxxxxxxxxxxxxx
I have a situation where I could

Try
Open a Connection
ExecuteReader
Close Reader
Catch

may not happen - Possible if this a Timer in a Windows Service

End Try

The problem is that the connection and/or the Reader may be open when I
get
to the "Open a Connection".

The Connection and Reader are Container objects in VS and are Global.

If the Sql Server is down, I may get bounced out of the Openning
Connection
or ExecuteReader statement and never get to the Catch because the delay
waiting for a response may be longer that the Timer delay.

So when I come back, how do I handle the Connect and Reader before the
"Open
a Connection" code?

I know I could do something like:

if SqlConnection1.State = "Open" then Close it

But what about the Reader?

Do I need to do something about it? I don't know if it is open or not.

Thanks,

Tom









.



Relevant Pages

  • Re: Close Reader or not?
    ... Would this be for both the Reader and the Connection? ... Sub Poller() ... But if I am sitting at ExecuteReader and Poller takes over, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Close Reader or not?
    ... Would this be for both the Reader and the Connection? ... What happens is if the Sql Server is down, I may be sitting at ExecuteReader ... Poller never finishes. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Close Reader or not?
    ... DataReader is Local and the Connection is Global ... Sub Timer_event ... and the Timer_eventre-executes and re-calls Poller and another DataReader ... close the reader and the connection BEFORE Poller attempts to open them ...
    (microsoft.public.dotnet.framework.adonet)
  • Do I need to close Reader if I close the Connection?
    ... may not happen - Possible if this a Timer in a Windows Service ... The problem is that the connection and/or the Reader may be open when I get ... waiting for a response may be longer that the Timer delay. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Close Reader or not?
    ... may not happen - Possible if this a Timer in a Windows Service ... The problem is that the connection and/or the Reader may be open when I get ... waiting for a response may be longer that the Timer delay. ...
    (microsoft.public.dotnet.framework.adonet)