• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between Entity bean and Stateful Session bean  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats is the difference between Entity bean and Stateful Session bean? Can anybody explain this with a example?
Are there any implementation differences or just because they inhert the specific class that they become Entity or Stateful Session bean?
 
Marc
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay,
an entity bean models a data item that you want to persist in a lasting way. This means that any changes to its data fields will be written to an underlying RDBMS either transparently (CMP) or using your own implementations of
store()/create() etc. methods if you're after a custom mapping.
For instance, an entity EJB within a banking system might be used to represent one customer, along with their details. You don't want to lose those details when the customer disconnects!
That persistence interval aspect is what makes entity beans different from stateful session beans. A shopping cart EJB will total up customer choices while that customer is on your shopping website. It needs to monitor session or "conversational" state for that well-defined interval which is 'one customer visit to my website'. However, unless you're after statistics for data mining (which is another question), you don't care for that data after the customer has left. Hence there is no need to persist it to a database: any session data gets discarded (or may get discarded) after the session ends.
Hope this helps,
Marc
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc,Your explanation is crystal clear.
I just have few more questions.
According to your explantion Shopping Cart is a example of Stateful Session bean, Can you give me a example for statless session bean. And Is it the methods implemtation in EJB which make EJB as Entity, stateful Session or stateless Session Bean?
Cheers Marc.
Vijay
 
Marc
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay,
A stateless session bean could have e.g. a credit card verification functionality - you hand in details, the bean checks whether your purchase can go through, returns a boolean and exits. Here a session is made up of a single invocation: the bean's service represents an atomic action. As soon as a session involves more than a single request, you need to save state.
A bean becomes an entity bean by implementing javax.ejb.EntityBean. Similarly, session beans implement javax.ejb.SessionBean. Also, make sure the fields in the deployment descriptor match the implementation of your beans.
For a stateful session bean, the developers have to keep track of the state themselves (by creating appropriate fields within the bean..e.g. Vector shoppingList for my earlier example). They also have to implement the ejbActivate() and ejbPassivate() methods since EJB containers may choose to passivate any beans as part of resource management..However, the single point which "decides" whether a session bean is stateful or stateless, as far as I can see, is the *session-type* field in the deployment descriptor.
Regards,
Marc
[This message has been edited by Marc (edited June 07, 2000).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!