Jun 27th, 2007, 07:05 PM
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.
Jun 28th, 2007, 05:30 AM
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.
Originally Posted by logicg8
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;
State : x==1; y==2;
so at the moment in Thread 1 x==0 and y==2, which corresponds to the state that has never existed.