EJB stateless session beans and stateful session bean
Asked Answered
M

4

4

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 ejbs and advantages over using plain java classes. Basically when do you go for stateless ejbs and when do you go for stateful ejbs. I want a real time application.

Mutilate answered 27/8, 2010 at 18:58 Comment(3)
you mean differences between stateless and stateful ejb?Caress
@shamsi not exactly but no theory answer.Mutilate
possible duplicate of Stateless and Stateful Enterprise Java BeansPtero
C
11

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).

Caress answered 27/8, 2010 at 19:17 Comment(4)
to maintain state we can use http sessionMutilate
@Suresh: yeah, that's true also, its up to you to choose the right place to keep the state.Caress
when do you go for http session vs stateful also statelessMutilate
@Suresh look at last part of my answerCaress
E
1
  • 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.

Elke answered 27/8, 2010 at 19:12 Comment(2)
i have read that everywhere. but i want to know when you will prefer , your experience in using them.Mutilate
we can use httpsession to maintain client state.Mutilate
C
0

Developers prefer to maintain state in web layer in modern enterprise applications. I have never seen a real world web application using Stateful Session Bean. It is a scalability issue also.

Chemoprophylaxis answered 3/3, 2013 at 1:58 Comment(0)
O
0

An example is a shopping cart stateful session bean that tracks a client's product choices and can execute a sale when requested.

Organology answered 21/11, 2014 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.