Exchanging results

Discussion in 'Java' started by Gianni, Jan 12, 2005.

  1. Gianni

    Gianni Guest

    >> Something really strange happen in my application
    >> run by Orion Application Server.
    >> Sometimes ( not always !) it happens that when 2 users
    >> do a query in the same time the user A get the data user B asked for
    >> and viceversa.
    >> I pass data with :
    >> request.setAttribute("tabledata", tabdata);


    >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 !
     
    Gianni, Jan 12, 2005
    #1
    1. Advertising

  2. Gianni

    Peter Kirk Guest

    "Gianni" <> skrev i en meddelelse
    news:...

    > 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?.
     
    Peter Kirk, Jan 12, 2005
    #2
    1. Advertising

  3. Gianni wrote:

    >>>Something really strange happen in my application
    >>>run by Orion Application Server.
    >>>Sometimes ( not always !) it happens that when 2 users
    >>>do a query in the same time the user A get the data user B asked for
    >>>and viceversa.
    >>>I pass data with :
    >>>request.setAttribute("tabledata", tabdata);

    >
    >
    >>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?


    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
     
    John C. Bollinger, Jan 12, 2005
    #3
  4. Gianni

    Sudsy Guest

    John C. Bollinger wrote:
    <snip>
    > ... 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.

    -------

    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...
     
    Sudsy, Jan 12, 2005
    #4
  5. Sudsy wrote:

    > John C. Bollinger wrote:
    > <snip>
    > > ... 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.

    >
    > -------
    >
    > 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
     
    John C. Bollinger, Jan 12, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rafael Veronezi
    Replies:
    0
    Views:
    421
    Rafael Veronezi
    Jul 2, 2003
  2. Gianni

    Exchanging results

    Gianni, Jan 11, 2005, in forum: Java
    Replies:
    2
    Views:
    378
    Peter Kirk
    Jan 11, 2005
  3. Peter van Schie
    Replies:
    2
    Views:
    431
    Peter van Schie
    Oct 2, 2005
  4. Amol
    Replies:
    2
    Views:
    454
    Valentin Tihomirov
    Oct 12, 2003
  5. Al
    Replies:
    2
    Views:
    450
Loading...

Share This Page