tags:

views:

53

answers:

2

I have gone through various books on stateful and stateless session bean and how they work.i want to know the real usage of these ejb beans and advantages over a plain java classes.basically when do you go for stateless and statefull .i want a real time application

A: 
  • Stateless session bean are lightweight: they do not store information about a specific user. They are usually used in a static way. For example a client ask for a product information will communicate with a stateless session bean. ("You want the price of product 'YXZ', here you go!")

  • Stateful session bean however remember's the client information. They contains data about the user actions. For example, let's say a user go through a shopping cart. The steps will be stored in a stateful session bean (for example, user it at the payment step).


You really need both type of session bean in any website. Unless you web site is so basic that anything can be done with stateless session bean (a read-only web site really).

Any web site that track a user through cookies, will need a stateful session bean. Be aware however that you can decide to put very little session information in a session bean and store that information in a database. But you still need some session management.

Thierry-Dimitri Roy
@thierry i have read that everywhere. but i want to know when you will prefer , your experience in using them.
Suresh S
@thierry we can use httpsession to maintain client state.
Suresh S
+1  A: 

the usage of these type of ejbs are usually in service layer as service classes.

EJB3 stateless and stateful bean are actually POJO (with some annotations) and they don't have any big difference with normal classes.

but in term of usage, they have some abilities that you can't find in normal classes like:

  • they can be called remotely (e.g. RMI protocol).
  • they can use application server context resources like DB Connection and Transactions.

stateless or stateful: - if a task or process can be done in a single step (by a single method call) stateless is the right option like a authentication process - if a task needs a series of method calls (more than one) and you need to keep previous results to use them in next call, then go for stateful. like a shipping process (select items, add/remove and then do the transaction)

http session or stateful?

ejbs can be served in application server and they may have different type of clients like a normal swing application or ..., so you can't relay on http session in these cases.

if your appserver and webserver are different (distributed) its not good idea keep data in http session and pass/getback it to/from app server (network overhead).

mohammad shamsi
to maintain state we can use http session
Suresh S
@Suresh: yeah, that's true also, its up to you to choose the right place to keep the state.
mohammad shamsi
when do you go for http session vs stateful also stateless
Suresh S
@Suresh look at last part of my answer
mohammad shamsi
@shamsi thanks for answerin i have got some idea.
Suresh S