0

I was checking EJB stateful beans, the typical example for using this bean is in a shopping cart.

So in the example what is done is check in HttpSession if the EJB exists, if not, one EJB instance is called and then upload to session using the HttpSessionId as identifier.

So, what is the advantage of using a stateful EJB when I could use a simple POJO to do exactly the same?

I guess the typical answer is that even if the HttpSession goes down the EJB still lives in the server container. If this is true, how can I recover that EJB from the EJB pool? given I don't have the HttpSessionID anymore.

Is there another approach to recover EJB's that are living in a container?

Eduardo
  • 2,070
  • 21
  • 26

1 Answers1

1

The shopping cart of a Stateful EJB hosted within an http session is in my opinion the typical example of something that shouldn't be done for a very simple reason: a shop online purpose is to increase traffic and number of (paying) users. A Stateful bean on the opposite is not recommended with this kind of web site, it doesn't scale.

Stateful Bean are something that should belong to the realm of desktop applications i.e. Swing, JavaFX etc... it has little to do with an www shop online.

Of course keeping things in a session is much lighter than having a Stateful bean holding data.

I cannot think of any trick to recover a Stateful EJB once you loose its reference.

Also, what do you mean with "if the session goes down" ? If you mean because of a server crash, keep in mind that if the EJB resides in the same server then an EJB (Stateless, Stateful, Singleton, etc...) cannot survive a server crash.

Leonardo
  • 9,607
  • 17
  • 49
  • 89