Static variables in httphandlers/httpmodules

G

Guest

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
 
H

Hans Kesting

Steen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top