467,903 Members | 1,809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,903 developers. It's quick & easy.

Static variables in httphandlers/httpmodules


This is what I'm doing:

I'm using Httpmodules and Httphandlers to control access to files (images,
movies, etc.), but have run into quite a problem when several files are being
requested at the same time, from the same client.
To avoid having to push my database connection around to all the methods of
the classes I've build, the connection is stored in a static variable.

When several files are requested from the same client, at the same time, I
get exceptions when accessing the database ("already an open datareader" and
the like), and I've even gotten a datareader with the wrong data in.
When I'm in debug mode it's a big problem, when I'm building a release
build, it's less a problem, but still there. Once the web application is
under stress, my guess is that it will be a major problem.
To solve this problem, I set the IsReuseable boolean on the HttpHandler to
false, but that didn't help anything.

I thought that a static variable would be static per request, and that
setting the IsReuseable boolean would help, but that is not the case??

Am I missing something, or is this normal behaviour? The same problem arises
when I'm using iframes and have a httphandler catching the aspx requests.

It looks like the content of static variables "survive" from one request to
another, can't this be avoided?? And even worse the static seems to be shared
among more than one request at a time.

Does anybody have any experience with this, and any solution to the problem?!?

tia,
Steen

Nov 18 '05 #1
  • viewed: 2366
Share:
1 Reply
Steen Tøttrup wrote:
This is what I'm doing:

I'm using Httpmodules and Httphandlers to control access to files
(images, movies, etc.), but have run into quite a problem when
several files are being requested at the same time, from the same
client.
To avoid having to push my database connection around to all the
methods of the classes I've build, the connection is stored in a
static variable.

When several files are requested from the same client, at the same
time, I get exceptions when accessing the database ("already an open
datareader" and the like), and I've even gotten a datareader with the
wrong data in.
When I'm in debug mode it's a big problem, when I'm building a release
build, it's less a problem, but still there. Once the web application
is under stress, my guess is that it will be a major problem.
To solve this problem, I set the IsReuseable boolean on the
HttpHandler to false, but that didn't help anything.

I thought that a static variable would be static per request, and that
setting the IsReuseable boolean would help, but that is not the case??

Am I missing something, or is this normal behaviour? The same problem
arises when I'm using iframes and have a httphandler catching the
aspx requests.

It looks like the content of static variables "survive" from one
request to another, can't this be avoided?? And even worse the static
seems to be shared among more than one request at a time.

Does anybody have any experience with this, and any solution to the
problem?!?

tia,
Steen


"static" is application-wide, so *everyone* accessing your application
shares the same single static variable.
Asp.net handles connection pooling itself, so don't try your own pooling.
The best way to use connections is to open them as late as possible,
closing them as soon as possible.
Having that connection in a static variable means that all requests
use just a single connection! So instead of gaining performance,
you lose it.
(storing the connection *string* in a static variable is not a problem)

A single connection can have just one open datareader. That's why you
got that error.

Hans Kesting
Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.