views:

71

answers:

2

I have a object that implements the HttpSessionAttributeListener, and as you'd expect it does some work when certain objects are added, replaced and removed from the session.

I thought that the if the session is ended [session.invalidate()], each object from that session is removed from the session as so the attributeRemoved() method would be called? I'm not seeing that behavour and wondered if I dreamt it.

To ensure I can be notified when the session is invalidated do I have to implement HttpSessionBindingListener on the object i'm interested in? or is there another way.

+1  A: 

When you call session.invalidate(), you're just informing the container that the session should no longer be used. It is not, however, obliged to do anything else, such as removing the session attributes. It will do that eventually, but there's no guarantee that it will do it immediately, and your application shouldn't rely on it happening in a timely manner.

skaffman
Thanks, do you know if session.invalidate() would cause container to notify session objects (that implement the HttpSessionBindingListener) that they are now unbound?
scottyab
It should notify them when they do get unbound, but that could happen any time after the session is invalidated, with no guarantees as to when.
skaffman
A: 

HttpSessionListener, implement the method sessionDestroyed

Bozho
When the sessionDestroyed you can't get any objects out the session, if you try i think you'd get sessionInvlaidException (or similar exception)
scottyab
Well, I haven't tried it, but having a look at Tomcat's code, it seems that listeners are called BEFORE the session is invalidated and attibutes are removed.
Bozho