File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Stateful Session Bean -- clarification urgent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Stateful Session Bean -- clarification urgent" Watch "Stateful Session Bean -- clarification urgent" New topic
Author

Stateful Session Bean -- clarification urgent

veena madhukar
Ranch Hand

Joined: Apr 28, 2006
Posts: 86
Stateful session beans are suggested to be used over Stateless session beans in the following context:

"The bean needs to hold information about the client across method invocations. "

1> Can't the information be stored in the web tier? When the web tier can store the information how does session bean get justified to be used.

2> Also, what kind of information about the client, can somebody give an example. I know the standard example quoted is Shopping cart. But, i am still not able to appreciate the use of stateful session bean. can anybody point me to any web sites or share information to help me understand better.

thanks in advance.
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
1> Can't the information be stored in the web tier? When the web tier can store the information how does session bean get justified to be used.

Yes, information can be stored on the web tier. (I assume you mean servlet container -- possibly in the Request or Session.)

Note that the statement said "hold information across method invocations" not "hold information across requests." Keeping a stateful session bean alive between http requests does not scale well because the bean holds expensive resources (memory, db connections, etc.) The session bean should be released once the processing of the request is complete.

The advantage to a stateful session bean is that you can call it's methods several times and don't have to pass the same state data on each method invocation.


2> Also, what kind of information about the client, can somebody give an example. I know the standard example quoted is Shopping cart.


Ok, using the shopping cart example...

Completing an e-commerce transaction may involve several steps, data sources and EJBs. Suppose you created a stateless session bean called CheckoutEJB. CheckoutEJB acts as a Facade to simplify creating an order. You also have 1 stateful session bean for order creation (CreateOrderEJB), 1 stateless session bean for credit card authorization (CCAuthEJB) and 1 stateless session bean to manage inventory (InventoryEJB).

For this example, checkout is a four step process.

1. you call OrderCreationEJB and pass the item information from the user's cart.
2. you call CCAuthEJB to authorize the credit card.
3. you update the inventory using InventoryEJB.
4. you commit the transaction using OrderCreationEJB.

If anything fails (credit card authorization fails, we suddenly discover we can't fulfill the order, etc.), the transaction is cancelled.

Since OrderCreationEJB is a stateful session bean, you don't have to pass all of the order data in step 4. He remembers all the data from step 1.

This is a contrived example, but it does show a situation where stateful session bean would be beneficial.
veena madhukar
Ranch Hand

Joined: Apr 28, 2006
Posts: 86
Thank you very much. It helped me understand better.

Can you please explain to me why we needed to create a stateful bean for createorder. We could have held all the orders in the httpsession and passed to stateless session bean. Now with stateful session bean we are storing it in the bean everytime the user selects an item to make order.
Ramasubbhu Allur Kuppusamy
Ranch Hand

Joined: Sep 16, 2005
Posts: 120
Scott,
Thanks for your expalanation.


Regards,<br />Ram.<br />SCJP 1.4
Ugender Rekulampally
Ranch Hand

Joined: Nov 14, 2005
Posts: 130
Thanks Scott for wonderful example.

Ugender
Chandra Sagi
Ranch Hand

Joined: May 05, 2005
Posts: 162
Originally posted by veena madhukar:
Can you please explain to me why we needed to create a stateful bean for createorder. We could have held all the orders in the httpsession and passed to stateless session bean. Now with stateful session bean we are storing it in the bean everytime the user selects an item to make order.


These are my thoughts on your question.

When you said to hold all the orders in a httpsession, we need to create a httpsession add objects to it, each time a new order is created and stateless bean should read from httpsession again. Instead using a stateful session bean the bean (the container) takes care of remembering the orders and makes it easy to work on them. Httpsession is obtained only when you have a web interface. Suppose you are using pure Java client how would you maintain the session? httpsession and sessionContext are different. Its easy to remember client information across method calls using sessionContext as in Stateful Session beans.

Hope this might be some help. That was a real cool example by Scott.

Thanks,
Chandu
veena madhukar
Ranch Hand

Joined: Apr 28, 2006
Posts: 86
Thank you for your explanation.

I am just trying to understand and not being caustic.
The example that Scott has given is the standard shopping cart example ...right. In that the 4 steps mentioned will not be separate method calls from the client. They all would be 4 steps within a method call from the client. Basically the client has selected items to be ordered. He gives credit card information. Both items and the credit card information will get passed to the application for processing with a single call.

The 4 steps will get executed under one transaction. Failure of any one of them would result in abotring the entire process.

I could understand the need for stateful session bean if the 4 steps were to be called separately as 4 methods from the client. In which case the client will have to remember and send data to the application. Instead of that storing the information in session bean would be more useful.

Veena
Chandra Sagi
Ranch Hand

Joined: May 05, 2005
Posts: 162
The example that Scott has given is the standard shopping cart example ...right. In that the 4 steps mentioned will not be separate method calls from the client. They all would be 4 steps within a method call from the client. Basically the client has selected items to be ordered. He gives credit card information. Both items and the credit card information will get passed to the application for processing with a single call.


The 4 steps are different method calls in different bean's as per the example. In general I don't think that items and credit card information will be passed in a single call. In any shopping website you would select your items first click checkout and then only you would be directed to give credit card information.

The 4 steps will get executed under one transaction. Failure of any one of them would result in abotring the entire process.


This is reasonable.

I could understand the need for stateful session bean if the 4 steps were to be called separately as 4 methods from the client. In which case the client will have to remember and send data to the application. Instead of that storing the information in session bean would be more useful.


You got it!

Thanks,
Chandu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateful Session Bean -- clarification urgent