aspose file tools*
The moose likes Servlets and the fly likes Clean-up objects in session. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Clean-up objects in session." Watch "Clean-up objects in session." New topic
Author

Clean-up objects in session.

Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Which listener to use?
SessionListener is not useful since its method is called after session is removed. SessionAttributeListener is not useful since in attributeRemoved() I still can�t get object in session. I got �Session already invalidated� message if I try Session.getAttribute() no matter the session expires or invalidated explicitly.
Any suggestions?
Thanks.


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29259
    
140

Bruce,
Are you trying to access an object in the session after the session is invalidated? These objects can be garbage collected at any time after the session is invalidated. What is in the object that you want to clean up?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
danny liu
Ranch Hand

Joined: Jan 22, 2004
Posts: 185
If you wanna release resource when a session is being invalidated, you can use HttpSessionBindingListener.
If a data implements HttpSessionBindingListener, it will be notified if a session is being invalidated. In the valueUnbound method, you can add some code to relese resource.
Hope it helps.
Dan
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thank you all.
I wrapped my object (including db connections) in HttpSessionBindingListener and it works.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29259
    
140

Bruce,
I would use a connection pool rather than keep connections in the session.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks.
I do store a connection pool in session.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29259
    
140

Bruce,
Why is the session instead of some static object? Connection pools aren't user specific data, which is the purpose of a session object.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3879
Jeanne has a point. The whole idea of a connection pool is that it should be SHARED across all the users. You're defeating the purpose by putting it in the Session.
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks for the response.
I my case, the connection is user specific. Each user has his own work mode such as schema set (in our AS/400 world, it is library list and it is included in the connection, believe or not).
I am coding applications for known users not for the web public at large.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clean-up objects in session.
 
Similar Threads
IlegalStateException
Invalidate Question
HttpSessionListener
IllegalStateException
Log Out Problem