0

I think I understood what is the difference between a Java Bean (JB) and an Enterprise Java Bean (EJB). There are a lot of answers to this question out there. But what I didn't really find an answer for:

If I code Java EE software with JSF, should I prefer to code everything (where I don't need CDI like for the models) with EJBs or only the things where I see an advantage in EJBs. For example because of perfomance if a Stateless EJB is possible or because I need transaction management?

For example I want to code an online calculator. The models for the view are CDIs. But what is with the classes which do the calculation on request of the models. Is there any reason to use EJBs for them?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
timmornYE
  • 708
  • 2
  • 8
  • 22
  • possible duplicate of [What use are EJBs](http://stackoverflow.com/questions/5579890/what-use-are-ejbs) – Raedwald Mar 04 '14 at 20:13

2 Answers2

1

Stateless session beans are pooled and are "assigned" to a single call from a client.

This means they are automatically thread safe and can hold on to expensive to create resources without the need to create those resources over and over again or have the bean in a shared scope (where it will be open to thread safety issues).

If you find yourself in a situation where you need this, stateless beans can improve performance.

They can also stabilize the performance of your system by virtue of the implicit request throttling that happens when using stateless session beans; if the pool is exhausted no new requests to the bean will be accepted. This can help with preventing an overload of your system. The calls that do get through will experience a better service, but some calls will get dropped. This is generally a better situation than all callers getting a crappy slow response.

Then there are a couple of services that are currently only available for usage in EJB beans, like the Java EE timer service. There have been requests to make this and other services available to all beans, but in Java EE 6 and even Java EE 7 it's EJB only.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
0

The main reason for EJB usage would be automatic transaction handling. If you're not performing database work, you wouldn't gain much from using an EJB. There is no performance advantage to using an EJB either.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Isn't a stateless Bean always more performant than a JB because the session has not to be hold? – timmornYE Jul 03 '13 at 10:58
  • Wotim: Blub? It look like you're not understanding the meaning of "session" in EJB context. It absolutely doesn't stand for HTTP session as you seem to think. Carefully read http://stackoverflow.com/questions/8887140/jsf-request-scoped-bean-keeps-recreating-new-stateful-session-beans-on-every-req/8889612#8889612 – BalusC Jul 03 '13 at 12:17
  • @BalusC, no, i know that they aren't a http session like they are in CDIs. This is not my confusion. My confusion is why a stateless EJB is not more performant than a JB, because it don't has to hold a session (no http session). – timmornYE Jul 03 '13 at 14:02