How to end a session with stateful session bean?

Discussions

EJB programming & troubleshooting: How to end a session with stateful session bean?

  1. Hi,
    I have made shopping cart using stateful session bean. I want that when the session expires the user's loginStatus field in a database table is set to 'N'.
    Can anyone please tell me how to catch this event of session getting expired. Is there any exception thrown or anything else?
    Please help me.
    Abhishek
  2. You could add a SessionEventListener (perhaps the interface's name is slightly different) to the user's session. This listener should detect the invalidation of the session and update the state of the user.
    Of course, this approach is not suitable for every application. In your case, I don't see how this has anything to do with the statefull session bean... unless you're storing the user's state in it.

    Cheers and happy coding,
    Martin
  3. Hi Abhishek,

    You can use the HttpSessionListener to capture the session create/destroy events. If you can keep the EJBObject handle of your stateful session bean in session scope, when session time out/invalidate haoppens, the sessionDestroyed() will be called. In sessionDestroyed(), write code to get the handle from session scope and call remove() method on EJBObject.

    Hope it may helps !!!

    Anil Sadineni.
  4. Hi, I used to use this solution before but sessionDestroyed() only perform if we call HttpSession.invalideSession(). If user close browser without sign out, sessionDestroyed() is not call?
  5. using ejbRemove()[ Go to top ]

    Well! I would like to add though i'm not sure..
    the life cycle method of a stateful session bean, ejbRemove will be called when the user disconnects in someway(anyway). so how about adding the code of updating the login status in the ejbRemove method of the session bean.. i think it should work.

    Regards,
    Vikas
  6. using ejbRemove()[ Go to top ]

    Well! I would like to add though i'm not sure.. the life cycle method of a stateful session bean, ejbRemove will be called when the user disconnects in someway(anyway).

    Hi Vikas,

    As per Sateful Session Lifecycle, the ejbRemove() will be called in two scenarios.

    1. Time out
    2. User calls the remove() method on EJBObject.

    In sessionDestroyed(), I can call the remove() method on EJBObject to remove the stateful session bean.
    so how about adding the code of updating the login status in the ejbRemove method of the session bean.. i think it should work.

    It will work. But, I can keep the code in another business method and call it from sessionDestroyed(). After that I call remove() on EJBObject. I think, its not good idea to keep that kind of code in ejbRemove().


    Anil Sadineni
  7. Hi, I used to use this solution before but sessionDestroyed() only perform if we call HttpSession.invalideSession(). If user close browser without sign out, sessionDestroyed() is not call?

    Hi Nam,

    There is no way to track when the user closes the browser window. But the container will call the sessionDestroyed() after the session timed out for that perticular user session.

    Anil Sadineni