Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Stateless Session Beans and Request Scoped beans

I know that Stateless Session Beans refers to EJB beans while Request Scoped refer to CDI beans (or JSF managed beans), so I will start by giving their both definitions from the Java EE Tutoriel.

Stateless Session Beans definition:

A stateless session bean does not maintain a conversational state with the client. When a client invokes the methods of a stateless bean, the bean’s instance variables may contain a state specific to that client but only for the duration of the invocation. When the method is finished, the client-specific state should not be retained.

CDI Request Scoped definition:

A user’s interaction with a web application in a single HTTP request.

What I understood from both definitions is that the concept of stateless session beans is the same as the request scope concept, but I'm not realy sure because the naming "session" confused me. So, I asked my self if this were the case (same concept) maybe they would have a similar naming (using request instead of session).

So, my question is: Is there any conceptual difference between EJB stateless session beans and CDI/JSF request scoped beans?

like image 929
Tarik Avatar asked Mar 05 '15 12:03

Tarik


1 Answers

There is a big difference.

A stateless EJB doesn't maintain state between invocations. It typically doesn't have state at all, except for dependencies on other beans. It offers a service to other components. The clients of a stateless bean can invoke it to serve an HTTP request, but they could also call it outside of any HTTP request (in a batch, a scheduled job, or whatever).

A request-scoped bean typically has a state (otherwise, it could be an application-scoped bean or a stateless bean). And this state lasts for the duration of an HTTP request. It can not be invoked outside of the HTTP request handling code. A good example for a request-scoped bean would be the current user:

  • a request comes to a servlet filter
  • the servlet filter extracts a cookie from the request and fetches the current user information related to this cookie from the database (by calling, for example, a stateless bean)
  • the servlet filter stores the user information into the "currentUser" request-scoped bean
  • the other components involved in the same request handling can get the current user information from this injected currentUser bean.
like image 114
JB Nizet Avatar answered Sep 30 '22 15:09

JB Nizet