Discussions

EJB design: Stateful Session Beans : how long is state preserved ?

  1. Hi,

    in my application I have a messagedriven bean connected to a topic. The bean gets a message, extracts some information, and then calls up it's servant, a stateful session bean. This stateful bean will execute some actions, have some state changes, and then return.

    One hour later, the messagedriven bean calls the stateful session bean again : is the state still there or will it be lost ?

    What about 2 days later ?

    How can I "tune" this ?

    Please, if you have advices, I need those who are not specific to one application server.

    Best regards, Jubin


  2. Well this depends on the Application server policy? the EJB Specs says:

    "The container’s caching algorithm may decide that the bean instance should be evicted from memory (this could be done at the end of each method, or by using an LRU policy). The container issues ejbPassivate on the instance. After this completes, the container saves the instance’s state to secondary storage."

    Cheers,
    Omar
  3. Yes...the state associated with your client (in this case messagedrivenbean) would be preserved by container hosting the stateful session bean as long as you hold remote interface reference in your messagedrivenbean. This behavior is independent of container implementation. If you don't want state to be preserved, try making the bean stateless or if you want stateful session bean then cache the home interface reference in your messagedrivenbean and acquire/release remote interface reference each time your OnMessage handler is called. But the latter may prove to be expensive depending on the way you implement ejbCreate().

    Hope this helps,
    Reema.
  4. Thanks a lot Reema,

    Well - I'm not sure if I understand you rightly.
    My concern is to preserve state in the stateful bean, even after the messagedriven bean terminates its OnMessage method and gets into the "does not exist" state.

    So that whenthe messagedriven bean is braught to life the next time, which may be in 2 days from now, the state of the stateful bean is still there.
    But if I chose your solution - caching the remote interface reference each time the OnMessage method is called - well errm... how can I keep the reference for the next time the messagedriven bean is called ???

    And how do I make sure the state of the stateful bean is still there ? I'm sorry, I don't quite understand your proposition.

    Best regards, Jubin Zawar
  5. Got you! Based on what I understand from your recent post, you would want to use EJB Object handles. They are basically persistent references to an EJB object. Which means that you can serialize this handle and then save it to some storage (before onMessage() call ends) and then later retrieve (even after 2 days! yes...) this handle from that storage, deserialize it and you are all set to continue with the conversational state of your stateful session bean. Major APIs to get this working: EJBObject.getHandle() and Handle.getEJBObject().

    Read EJB specification for further details.

    Hope this helps,
    Reema.
  6. The container has the right to timeout SFSB, regardless the exists of reference/handle.
  7. quing is right. The container first passivates the state after a deployed timeout and then at least he releases the state after a further timeout sequence, regardless whether there exist references or handles. I think you have to choose an entity bean solution for your need to hold the state for at least 2 days in saving it to a persistent storage.

    Regards
    Michael
  8. I don't think the state of the session bean will still be there after two days... at least I wouldn't count on it! I would take a snapshot of whatever you are doing in the session bean and either serialize it to disk or store the value of the fields somewhere in a database table. Then you could read it back in to get back to where you were statewise two days ago.

    Greg.