• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clean-up objects in session.

 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
danny liu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.
I wrapped my object (including db connections) in HttpSessionBindingListener and it works.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruce,
I would use a connection pool rather than keep connections in the session.
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
I do store a connection pool in session.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic