Question on Session Listeners in servlets

Discussions

Web tier: servlets, JSP, Web frameworks: Question on Session Listeners in servlets

  1. Hi All,
    Are the Session listener events synchronous or asynchronous??.

    Can the Listener event class be used to interact with the database.

    For example, when i set an attribute to a session, the container will invoke the attributeAdded method of the listener class. Now i want to store the session data in a database.
    can this process of saving to the DB be done inside the attributeAdded method of the listener. is this a proper design.

    Also do the container call the listener's method from the same thread of execution of the client. (i.e) a request will have a thread to access the servlet. If we set a value in session, the container has to invoke the listener. So does the container do this within the same thread or it uses different threads to invoke the listeners asynchronously??

    Thanks in advance,
    Chandru ram
  2. 1. Can the Listener event class be used to interact with the database?

    Sure. The Listener event class can do just about anything. It is kind of an unusual design to perform database updates for "attributeAdded" events, though.

    2. Are the Session listener events synchronous or asynchronous?

    The specifications are vague. I believe in practice they tend to be synchronous, but this is not something you can count on.
  3. hi,
    what is the good design methodology to keep the session persistent
    even after system crash / restart using listeners??
    thanks
    Jaideep Deshmukh.
  4. hi,

    > what is the good design methodology to keep the session persistent
    > even after system crash / restart using listeners??

            Iam really looking for a good design pattern to achieve this. Has someone done this in their applications.
  5. If the server crashes, odds are very good that you session listeners are going to fail as well.

    If you have any data that you don't want to lose, store that data in the database, not in the session. That is the only way to be 100% safe.