473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Database connection object in shared variable

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
Nov 18 '05 #1
10 2483
Hi Daniel,

I'm going to have to disagree with your premise:
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.
"Can't" is not a word that should be in any programmer's vocabulary. Of
course you can. You just don't know how to.

Based upon the corrected premise, the entire question is moot. I would
suggest asking instead, how you CAN work with a Connection in the way it was
designed to be used.

In other words, what is it that seems to prevent you from using a Connection
in the way it was designed to be used?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Daniel Fernandes" <da**************@sesame.co.uk> wrote in message
news:eb************************@posting.google.com ... 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

Nov 18 '05 #2
eIt will happen any time a connection still has an open object appended to
it. This could be a long running command, or the issue you are talking about
(open Reader).

You can test the type of Exception thrown, however, and determine if it is a
execution permission. Check the exception objects in System.Data. In
addition, you can pull the errors collection when you are using SqlClient and
query the errors returned (SqlErrorCollection class).
---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

"Daniel Fernandes" wrote:
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

Nov 18 '05 #3
On 29 Sep 2004 07:55:06 -0700, da**************@sesame.co.uk (Daniel
Fernandes) wrote:

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 possible" thing
but in that case I can't.


Hi Daniel:

By any chance are you trying to restrict the number of connections to
the database? If so, you could adjust the connection pool size in the
connection string.

Perhaps if you tell us why you must use a shaed connection we could
think of alternatives. I would try to avoid the current implementation
at all costs. Threading bugs tend to be subtle and sometimes remain
latent until the worst possible time. Just ask GE Energy!
http://www.securityfocus.com/news/8412

--
Scott
http://www.OdeToCode.com/
Nov 18 '05 #4
When having more than one DataReader on an open connection (though it looks
like you don't see this, it's likely because of something wrong in your test
such as IIS6 new application isolation mode perhaps). Though you could
workaround this is IMO really looking for trouble.

What is the reason behind having a single shared connection ?

Patrice

--

"Daniel Fernandes" <da**************@sesame.co.uk> a écrit dans le message
de news:eb************************@posting.google.com ...
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

Nov 18 '05 #5
I do know how to create a data access class that uses connection
pooling.

The problem is that I have to use an internal library that provides
helper classes for data access and in particular there is a
storedprocedure object that has two constructors :

1. Where you pass a connection (not a standard sql connection though)
and you would expect the connection to be opened, the stored procedure
to be executed and then the connection to be closed. Sadly the
connection is never closed which means after 100s or so calls SQL Server
will stop responding. I didn't look at the implementation but my guess
is the developer uses IDisposable for disposing the connection object
and this is obviously a mistake because the connection should be closed
and disposed explicitely after the stored procedure execution.

2. The second constructor is very similar to 1. and it's expected that
the connection is kept alive at the end of the stored procedure
execution. And that is the only one I am using but I would think such a
mechanism is only safe when in a single thread type application.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #6
I finally managed to replicate the problem by having a stored procedure
returning a large number of rows (5000+) and running multiple ASPX pages
that did the very same thing.

I had again the "There is already a datareader associated with this
connection which must be closed first." exception.

Although I am not using datareaders I know they are used internally used
as a dataset is filled by a dataadapter that makes use of datareaders.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #7
Hi Daniel,

I'm not sure what you mean by "an internal library." Internal to what? Do
you mean that somebody else in your company developed the library? If so,
that person is the one who should be posting here, as their design is
fatally flawed. IOW, the problem lies with the library you're using. I can't
imagine why anyone would be forcing you to use defective software. To build
good software, you need to USE good software.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Daniel Fernandes" <da**************@sesame.co.uk> wrote in message
news:#k**************@TK2MSFTNGP15.phx.gbl...
I do know how to create a data access class that uses connection
pooling.

The problem is that I have to use an internal library that provides
helper classes for data access and in particular there is a
storedprocedure object that has two constructors :

1. Where you pass a connection (not a standard sql connection though)
and you would expect the connection to be opened, the stored procedure
to be executed and then the connection to be closed. Sadly the
connection is never closed which means after 100s or so calls SQL Server
will stop responding. I didn't look at the implementation but my guess
is the developer uses IDisposable for disposing the connection object
and this is obviously a mistake because the connection should be closed
and disposed explicitely after the stored procedure execution.

2. The second constructor is very similar to 1. and it's expected that
the connection is kept alive at the end of the stored procedure
execution. And that is the only one I am using but I would think such a
mechanism is only safe when in a single thread type application.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #8
Well, there is actually one other possibility. I seem to recall you saying
that you had problems with an opened connection associated with a
DataReader. A DataReader is a connected object. It must maintain an opened
connection to the database for the duration of the time you use it. It can
only fetch one record from the database at a time. I have developed a Data
Access class for our company, and it includes methods for getting
DataReaders. You have to pass a Connection and Command reference to it, so
that you can close them afterwards. And when you use the DataReader, you'd
darned well better close the Connection yourself afterwards. Which is why I
also created a method to which you pass a Connection, and it closes and
disposes the Connection.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Daniel Fernandes" <da**************@sesame.co.uk> wrote in message
news:#k**************@TK2MSFTNGP15.phx.gbl...
I do know how to create a data access class that uses connection
pooling.

The problem is that I have to use an internal library that provides
helper classes for data access and in particular there is a
storedprocedure object that has two constructors :

1. Where you pass a connection (not a standard sql connection though)
and you would expect the connection to be opened, the stored procedure
to be executed and then the connection to be closed. Sadly the
connection is never closed which means after 100s or so calls SQL Server
will stop responding. I didn't look at the implementation but my guess
is the developer uses IDisposable for disposing the connection object
and this is obviously a mistake because the connection should be closed
and disposed explicitely after the stored procedure execution.

2. The second constructor is very similar to 1. and it's expected that
the connection is kept alive at the end of the stored procedure
execution. And that is the only one I am using but I would think such a
mechanism is only safe when in a single thread type application.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #9
Kevin

Thanks for your answer.
By "Internal Library" I meant a framework that have been developed for
internal use and that indeed has some issues when dealing with
connections.

The reason why I am getting an exception related to a Datareader object
is because the Fill method of a DataAdapter I believe uses a Datareader
hence the problem.

I am doing some tests using ACT and I hope I will be able to convince
the maintainer of the library to update it.

Cheers
Daniel


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #10
Hi Daniel,

Interesting. Our in-house data class has methods for working with DataSets
as well. However, these methods open their own Connection, create the
DataSet, and then close the Connection. The user of the method doesn't even
have to think about the Connection. In fact, I have found that it is best to
use Connections internally (inside a method) whenever possible, rather than
exposing them to the developer, who may or may not be good enough to know
what to do with them. The only methods we have that expose Connection
objects are ones that return DataReaders.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Daniel Fernandes" <da**************@sesame.co.uk> wrote in message
news:em**************@TK2MSFTNGP15.phx.gbl...
Kevin

Thanks for your answer.
By "Internal Library" I meant a framework that have been developed for
internal use and that indeed has some issues when dealing with
connections.

The reason why I am getting an exception related to a Datareader object
is because the Fill method of a DataAdapter I believe uses a Datareader
hence the problem.

I am doing some tests using ACT and I hope I will be able to convince
the maintainer of the library to update it.

Cheers
Daniel


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Larry L | last post by:
Access is noted for bloating a database when you add and delete records frequently. I have always had mine set to compact on close, and that works great. Now after everyone's advice I split my...
3
by: Grigs | last post by:
Hello, I have a web service that reads its web.config file to connect to an Oracle database. There are a number of methods in this socalled BACKBONE that either send inforomation to or from the...
15
by: Rob Nicholson | last post by:
I'm starting to worry a bit now. We're getting the above error when two users hit the same database/page on an ASP.NET application using ADO.NET, talking to a SQL 7 server. The error is perfectly...
10
by: Nick N. | last post by:
Hi all, I just started to play around with VB.Net. I currenlty do Powerbuilder work and was wondering how database connection management works in VB.net. My applications typically pop-up a...
2
by: Ron St-Pierre | last post by:
We're developing a java app and are using postgres as the database. On our dev server I started the app, closed it, but the java process was still open so I killed it, which caused the above error....
10
by: jt | last post by:
The program works like this: There is a form with a button. When the form is loaded, a separate thread is started which is retreiving/updating data in the database every x seconds. When clicked...
8
by: Maxi | last post by:
Hello, i'm sorry my bad english :( I have CR9 Webservice, how to change databadse name and User_name into Webservice method? (not Viewer Control) Tks!! -- --------------------------
0
debasisdas
by: debasisdas | last post by:
DATABASE LINK =============== A database link is a path through which a remote user in another database can connect to any other database. Once created database link exists as an object in the...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.