How to tell if a datareader is open on a connection and close it

Posted by Earlz on Stack Overflow See other posts from Stack Overflow or by Earlz
Published on 2009-11-12T15:53:12Z Indexed on 2010/04/30 2:27 UTC
Read the original article Hit count: 491

Filed under:
|
|
|
|

Hi, I'm setting up a little thing for connection pooling and on of the more common bugs that we have occur(its always an easy fix, but we just can't remember reader.Close()!) it is when we have a connection that is used by a lot of classes/methods and one method opens a data reader and forgets to close it. This isn't really bad cause a lot of times all you have to do is go into the debugger and go up one level and see the function before it was and check if it had an unclosed data reader.

Now, here is the bigger problem. In this connection pool, if a datareader is open, then it's not known until a thread gets a connection and tries to use it and the thing that originally opened the data reader may no longer even be alive.

So quite simply, how can you detect if a data reader is open on a connection and is there a way to close the reader without closing the connection?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET