Announcement Announcement Module
Collapse
No announcement yet.
Need Modifiable State in Singleton Bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment

    Working...
    X