Results 1 to 2 of 2

Thread: Need Modifiable State in Singleton Bean

  1. #1
    Join Date
    Jul 2006
    Posts
    19

    Default Need Modifiable State in Singleton Bean

    I have a requirement to maintain state in memory within a web application. The state needs to be shared among HTTP sessions. Although this can be done using a singleton, thread safety is a major concern. What is the best course of action to use the singleton for maintaining state across sessions but also ensure thread safety? I suppose making each method synchronized that alters state of the singleton will accomplish what I need. Or are there issues there as well? Thanks.

  2. #2
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Quote Originally Posted by logicg8 View Post
    ... I suppose making each method synchronized that alters state of the singleton will accomplish what I need. Or are there issues there as well? Thanks.
    It may be not enough (depending on nature of the state and the methods that alter that state). If methods that read the state are not syncronized with īmethods that alter the state it is ipossible to read partially-altered state.

    Moreover, if methods that read the state read it piecewise it is possible to read state that has never existed. E.g.

    State : x==0;y==0;

    Thread 1
    x=state.getX();
    Thread 2;
    state.setX (1);
    state.setY(2);
    State : x==1; y==2;
    Thread 1:
    y=state.getY():

    so at the moment in Thread 1 x==0 and y==2, which corresponds to the state that has never existed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •