It's not a secret anymore!*
The moose likes EJB and other Java EE Technologies and the fly likes Stateful session bean maintaining conversationsal state Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Stateful session bean maintaining conversationsal state" Watch "Stateful session bean maintaining conversationsal state" New topic
Author

Stateful session bean maintaining conversationsal state

anagha patankar
Ranch Hand

Joined: Dec 26, 2005
Posts: 53
Hello ,
Not talking about ejb3.0 am still using ejb 2.0 .
We are using stateless session beans .

However I am curious how stateful session beans maintain state .
Mine is a web application where if client makes two seoarate requests :
1 >First request from client is supposed to add some item to a shopping cart

2 >On any subsequent action by client - essentially state should be maintained in stateful session bean .

My question is -
1 >how does EJB container know which bean needs to be invoked ?
2 >If the client has put an item "A" in his shopping cart - shere is the item "A" maintained ?

Thanks in advance ,
-anagha
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
You'll have to keep the reference to the stateful session bean at some place like user's session. Otherwise if you loose the reference, it will remain in the pool and will be swiped out after sometime.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
A stateful session bean has accessor methods which the client uses to store and retrieve data. Should the bean be passivated, the EJB container will store its state. This may be done by attempting to serialize any fields that are not declared transient. The actual means of storing the bean's state varies: it may be the file system, database or in-memory.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
anagha patankar
Ranch Hand

Joined: Dec 26, 2005
Posts: 53
Thanks Ali and Roger for your answers .

From the replies what I understand is lets say I have an ArrayList as an instance level variable .
As an example ( Please ignore the incorrect names of the Interfaces that a Sessionbean needs to implement - just trying to come up with a scenario .



My doubts being :

You'll have to keep the reference to the stateful session bean at some place like user's session. Otherwise if you loose the reference, it will remain in the pool and will be swiped out after sometime.


So if I use the HttpSession to store the reference of sesion bean :
1 >should I store the Handle of : EjbHome or EjbObject ?
2 >Can I store the EjbHome or the EjbObject itself

3 >So once I get the reference to my Bean - I can access the state which is maintained ( in example in ArrayList called "empList" ?

Thanks for all the help.
Regards,
-anagha
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
When a client obtains an EJB object through a remote home interface, it is actually given a reference to a stub that implements the remote interface for the bean. The remote interface defines the methods which are exposed to the client. Normally, the client only needs this reference to make repeated calls on the bean's methods.

As for Handles ... I don't see much point in storing a HomeHandle because any client can get a reference to the home object by doing a JNDI lookup. However, there can be a reason for storing a serializable Handle which identifies an EJB object. If you store the Handle which references an EJB within an HttpSession object in a servlet, then this would allow successive client interactions with the servlet to use the same EJB.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateful session bean maintaining conversationsal state