• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stateful Session Bean question  RSS feed

 
Kirk Maze
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a MVC J2EE program which is a distributed calculator. Since we need to be able to persist client entries for an entire session, I chose a Stateless Session EJB for my business logic. However, I am getting a brand new EJB without the persisted data everytime I click a button on the JSP view. I thought I'd get the same EJB everytime I hit the model from the same client. Where is my misunderstanding?
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kirk Maze:
... I chose a Stateless Session EJB for my business logic. ...

Did you mean to say stateless here?
Simon
 
Kirk Maze
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously Not! My design utilizes a STATEFUL Session Bean. And I would think I would get the same bean back every time I hit it from the same client, but I'm getting a new bean. I'm running WSAD 4.0, and I'm wondering if I'm getting some persistence issue relating to it, but I'm not throwing an exception of any sort. So I did this (mis)post, just to see if there was something fundamentally wrong with my understanding of stateful session beans.
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The client needs to keep a reference to the Handle of the Stateful Session Bean. For web clients this reference is typically stored in the HttpSession. In subsequent requests the client can then use the Handle to retrieve the Stateful Session Bean instance by calling Handle.getEJBObject().
 
Kirk Maze
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris - I'm not now doing that, I'll give it a try.
 
Kirk Maze
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went to Sun's shopping cart tutorial, and it doesn't appear that the Bean is storing a reference to itself on the HttpSession object (is this happening implicitly?) and, more importantly, the client program (which is a Java app) doesn't seem to be using a handle anywhere to get the same stateful session back. What am I missing? Does anyone know of sample code with a web client which uses the technique Chris is referring to?
 
Brian Stelzer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are getting your STATEFULL session bean are you using the same session home each time? If you look up the session home, and then use that to get the session object everytime you will end up with a different instance everytime.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. You can't just go and look up the stateful bean a second time. You have to hold on to the reference (or a handle) through the lifetime of your client application.
Kyle
 
Kirk Maze
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still struggling with this. If I store the Home reference, it would have to be on the controller, which is the servlet. But the lifespan of a servlet is not the same as the lifetime of a session, which means that any other clients that hit the servlet during its lifetime are going to get the same old, and for them, erroneous, session. Help!
 
Kirk Maze
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't see this. Do I have to write a Factory to build the Home object and have some sort of map that maps home objects to clients? If it's that complex, what's the point of using a Stateful EJB in the first place? It seems that it would be simpler to use Stateless beans, and write a custom JavaBean to encapsulate the state dependant variables and simply pass that bean back and forth between the client and the model on every call.
But I can't believe this is the case - I must be missing something. Comments?
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kirk Maze:
Do I have to write a Factory to build the Home object and have some sort of map that maps home objects to clients?

This is already done for you in J2EE. It is called the HttpSession. An HttpSession is tied to a client, thru the use of cookies or url rewriting. All you need to do is place the Stateful Session Bean reference or Handle in the user's session.
 
Kirk Maze
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris!! That post somehow got through my thick skull when your earlier one didn't! Amazing how you can sweat over three lines of code for two days sometimes!
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One last thing -- it's not the home reference you want to store in the HttpSession -- it's the actual EJB reference (what comes back from the create() method you send to the home) that you want to store. Storing the home won't do you a lick of good -- you want to hold on to the same actual EJB reference to keep the link to your data around.
Kyle
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!