• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Stateful Session Bean -- clarification urgent

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott,
Thanks for your expalanation.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Scott for wonderful example.

Ugender
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic