Exchanging results

G

Gianni

Something really strange happen in my application
It depends a lot on how you generate and return your data. Maybe you have a
servlet which generates some data and stores it temporarily in an instance
variable before returning it to the user.
Then it's possible:
user A requests some data
user B requests some data
the servlet generates the data for user A and stores it
the servlet generates the data for user B and stores it (overwriting the
previous data)
the servlet returns the data to user A (which was actually generated for
user B)
the servlet returns the data to user B
and all sorts of variations of the above.

You are perfectly right. The value of the servlet change end they exchange results !
But how to fix it?
Thanks !
 
P

Peter Kirk

You are perfectly right. The value of the servlet change end they exchange results !
But how to fix it?
Thanks !

You really need to provide a little more information on exactly what your
program is and how it functions. Maybe some code?.
 
J

John C. Bollinger

Gianni said:
You are perfectly right. The value of the servlet change end they exchange results !
But how to fix it?

It depends greatly on the specifics of your implementation, but the
first thing to understand is that this is a thread safety problem.
Application servers are multi-threaded, and servlet implementations need
to be thread-safe. Going for the lowest-hanging fruit first, then: do
not store any request-specific data in instance variables of the
servlet, in static variables of any class, or as attributes of the
ServletContext or the session. Storing request-specific data in any
object (a List or array, for instance) makes that object
request-specific as well. You can safely pass around request-specific
data via method parameters and return values, or by storing them in
attributes of the relevant ServletRequest object.

If avoiding those potential problems does not solve the problem then you
are probably going to have to show some code for us to render any help.


John Bollinger
(e-mail address removed)
 
S

Sudsy

John C. Bollinger wrote:
... do
not store any request-specific data in instance variables of the
servlet, in static variables of any class, or as attributes of the
ServletContext or the session.
 
J

John C. Bollinger

Sudsy said:
John C. Bollinger wrote:



-------

Why would you say that, John? The session is /the/ correct place
for storing relevant information as it relates to a particular
user interaction. Maybe I'm misunderstanding something...

It is possible to have multiple requests in the same session in flight
at the same time. How likely that is depends on the webapp, but imagine
an app using frames, or providing in-page images via servlets, or
accessed via a portal application such as JetSpeed. Or just a vanilla
webapp on which a user has two browser windows/tabs open at the same time.

If you have data that really is specific to a particular request then
you should not store it in the session. Use the request object instead
-- that's why it has attributes. There's also the convenience of not
having to clean up session attributes after request processing. For
rigorous thread safety you need to be very careful about how you
manipulate state that is shared across requests, which includes all of
the items I mentioned in my earlier post.


John Bollinger
(e-mail address removed)
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,186
Latest member
vinaykumar_nevatia

Latest Threads

Top