• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

session.removeAttribute()

 
gurusamy sengodan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have added one field by using session.setAttribute("DBID",dbName);
Once use it
Is it necessary to remove the attribute by using session.removeAttribute()?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know you're done with it and won't need it again, it's not a bad idea to explicitly remove it; especially if it's large or contains references to many other objects.

If you don't remove it, it will be removed when the session is destroyed.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are done using an attribute, it's always polite to clean it up and remove it from the session.

I did notice the DBID as the attribute. If the attribute being put in the session is used universally for all users accessing that application, you might want to consider putting it into the ServletContext, as opposed to the session. With a session, the attribute is duplicated for every user. If it's put into the context, every user shares the same value, which is very efficient.

Just a thought.

-Cameron McKenzie
 
Chaitanya Devavarapu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't have future requirement with that object, you can remove it
by using
session.removeAttribute();
Its not a bad practice.

If you do not remove it at present also,there is no problem.

Because you are storig the object in session, whenever your session
time-out is over, then the session object is automatically removed by
the web-container.
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its definitely a good idea to remove it once its no longer in use. House-keeping should be done else you may run out of memory.

If you're dealing with clustering, the more objects you store in the session, the more work the server has to do to replicate the session. And all these add overheads to your server and could affect your application performance.

Unless very necessary, try putting it into the request object, else your application may not be that scalable.
 
vishnu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its good habit to delete that all but it is not compalsary to do that.when ever you are session is destroy that session will also removed.

Thanks
vishnu.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"vishnu",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted, often without warning

thanks,
Dave
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"vishnu", that was your third and final warning. Change your display name as directed prior to your next post or your account will be disabled.

bear
JavaRanch Sheriff
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
it's not a bad idea to explicitly remove it


what about the session.invalidate() method ? can we use this instead of removeAttribute() please give us some valuable suggestion to handling with session.

Thanks in Advance,
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
session.invalidate() will invalidate the whole session, including all scope variables set within it. Sort of like using dynamite to open a jar of pickles.
[ December 10, 2006: Message edited by: Bear Bibeault ]
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks bear,

so you mean to say session.removeAttribute() is more better choice instead using session.invalidate() method.

so where this method is useful?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Saif uddin:
so you mean to say session.removeAttribute() is more better choice instead using session.invalidate() method.


Invalidating the whole session in order to remove an attribute isn't just "not better", it's destructive and incorrect.

so where this method is useful?


That's a completely diffrernt question than this topic, so please start a new topic if you have questions about session invalidation.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks bear,

now it's completely clear to me..

Thanks again..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic