EJB stateless session beans and stateful session bean

巧了我就是萌 提交于 2019-11-30 10:09:18

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

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

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!