• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bad idea to store user-defined objects in sessions?

 
Buzz Andersen
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I've read through this forum's archives and loads of other stuff on the Internet, and I'm still in a quandry about a fundamental question: is it or is it not good practice to put "user-defined" objects (such as JavaBeans) in a session via session.setAttribute()?
BEA's WebLogic documentation seems to discourage this practice strenuously, arguing that only standard Java objects (such as Strings and Collections) should be put in a session. Other places (such as jGuru's Servlets FAQ) advocate using JavaBeans in sessions as an excellent way to store things like result sets across requests.
Besides serialization issues, what is wrong with keeping a bean as an attribute in a session? Is BEA's view widely held by developers?
Thanks in advance for your help in clearing this up in my mind :-)...
Buzz Andersen
[This message has been edited by Buzz Andersen (edited October 24, 2001).]
[This message has been edited by Buzz Andersen (edited October 24, 2001).]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Buzz Andersen:
is it or is it not good practice to put "user-defined" objects (such as JavaBeans) in a session via session.setAttribute()? BEA's WebLogic documentation seems to discourage this practice strenuously, arguing that only standard Java objects (such as Strings and Collections) should be put in a session.
What reason do they give? In all of the stuff I read, and the systems I built, it is the rule rather than the exception to bind JavaBeans in the session. I don't see how you can build a maintainable OO system if you try to shoehorn all your state inside a few amorphous collection classes.
- Peter
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the main point is to make sure that any object you put in a session is Serializable - that way the servlet engine can write it to disk when memory is low or share it in a server farm.
Bill
------------------
author of:
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds a bit like throwing the baby out with the bathwater, doesn't it? I can't believe that BEA would make such a bone-head stupid suggestion. Let's all just abandon OO programming, shall we?
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From experience using the Weblogic servers, I think they're leading from the fact that the way ClassLoaders are handled in their (and possibly others) products, storing user defined objects on the session can cause ClassCast problems. (oh the fun we've had with this...)
For the record I'm strongly in the camp of "use the session lightly, only put data on the session that is true for the session". I've worked with others who put 'transitional state' on the session and seen the mess you can cause. Typically I put a user object and a shopping cart object and thats usually it for the session.
(also for the record, another point I'd like to make is that there is a league of difference between William and Kyle and the ppl I worked with. From "Best Practices" I say don't use the session, although experienced web app probrogrammers are less likely to fall into the same holes)
Dave.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that you should use the session lightly. It's not a dumping-ground for everything in your app. Your shopping-cart example is a good one.
However, just storing primitives because they won't let you store other objects without problems is going a bit far. Sounds like a good reason to go Application Server shopping to me
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
Buzz Andersen
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your replies! For the record, the document I was reading is here:
http://www.weblogic.com/docs45/classdocs/API_jsp.html#guidelines
I believe that David O'Meara is correct that their concern is what they call the "dreaded ClassCastException" (a problem that I will definitely admit can be excruciating when you encounter it). It just seemed to me that they were adopting a rather scorched earth solution to the problem, and I wanted to make sure I wasn't doing some bonehead thing that more experienced developers would scoff at by storing beans in the session :-).
Buzz
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!