The moose likes Servlets and the fly likes session timeout and finally method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "session timeout and finally method" Watch "session timeout and finally method" New topic
Author

session timeout and finally method

A Saari
Greenhorn

Joined: Mar 31, 2006
Posts: 26
Does anyone know ... when the web server times out a session I'm assuming all instances created by that session end up being garbage collected at some point - right? And when that happens the finally method gets called for each instance - right?

I have a static list containing locked projects (i.e. locked for edit in some browser somewhere) and if someone gets up and walks away I need to release that lock (in the finally method right now).

Will the above work correctly?

Does someone know of a more elegant soln?

Thanks,
Amy
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

It would be better, in my opinion, to use a session listener to react to a session going out of scope rather than relying on garbage collection.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

when the web server times out a session I'm assuming all instances created by that session end up being garbage collected at some point - right?


It would be more appropriate to say objects bound to session scope are eligible for GC once the user session times-out. Objects are NOT created by session, objects are bound to session.



And when that happens the finally method gets called for each instance - right?


We don't no when GC runs so we don't know when finally method will be executed and also the order is also NOT guaranteed.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Also note that the objects may have another reference somewhere - possibly in a collection - thus preventing them from ever being GCed.
So - like Bear said - use one of the listener interfaces to ensure correct handling. For example, have the object that holds the lock implement HttpSessionBindingListener, then the valueUnbound method is guaranteed to be called when the session is invalidated.
Bill
A Saari
Greenhorn

Joined: Mar 31, 2006
Posts: 26
Done.

Thank you both *very* much...

Amy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session timeout and finally method