D
Daniel Fernandes
Hi there
Let's say due to several reasons I have an ASP.Net application that
has a database access class that stores a database connection in a
shared member :
public class dataaccess
private shared DB as System.Data.SqlClient.SqlConnection
public shared function GetItems as dataset
...
end function
public shared sub InsertItems as dataset
...
end sub
end class
I understand that I shouldn't be doing this and instead do the "open a
connection as late as possible and close it as soon as possible" thing
but in that case I can't.
I also undestand that putting database connection object in a shared
variable can lead to problems because access to shared members is not
thread safe.
Now...Only two times I had such a "predictable" issue happening and
the exception thrown was "There is already an open DataReader
associated with this Connection which must be closed first.".
I tried to programmatically replicate this by either running calling
multiple pages that will execute a stored procedure containing a
WAITFOR command or that execute a stored procedure that returns
thousands of records.
In both cases I didn't manage to get the exception thrown.
Which lead to my question which is: under which circumstances the fact
of using a database connection object in a shared member will lead to
predictable problems ?
PS: I believe that in the two times where I had the exception "There
is already an open..." thrown was when the execution of the stored
procedure couldn't be performed and therefore resulted into an
exception due to other issues (execute permission for instance).
Thanks for your help
Daniel
Let's say due to several reasons I have an ASP.Net application that
has a database access class that stores a database connection in a
shared member :
public class dataaccess
private shared DB as System.Data.SqlClient.SqlConnection
public shared function GetItems as dataset
...
end function
public shared sub InsertItems as dataset
...
end sub
end class
I understand that I shouldn't be doing this and instead do the "open a
connection as late as possible and close it as soon as possible" thing
but in that case I can't.
I also undestand that putting database connection object in a shared
variable can lead to problems because access to shared members is not
thread safe.
Now...Only two times I had such a "predictable" issue happening and
the exception thrown was "There is already an open DataReader
associated with this Connection which must be closed first.".
I tried to programmatically replicate this by either running calling
multiple pages that will execute a stored procedure containing a
WAITFOR command or that execute a stored procedure that returns
thousands of records.
In both cases I didn't manage to get the exception thrown.
Which lead to my question which is: under which circumstances the fact
of using a database connection object in a shared member will lead to
predictable problems ?
PS: I believe that in the two times where I had the exception "There
is already an open..." thrown was when the execution of the stored
procedure couldn't be performed and therefore resulted into an
exception due to other issues (execute permission for instance).
Thanks for your help
Daniel