Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does @SessionScoped work with EJB? Is CDI only for web-tier?

How is the session defined in @SessionScoped CDI bean?
Is this annotation valid only when called from Servlet container, where the session is well defined in form of HttpSession?

If not, than how an EJB with @Inject @SessionScoped MyBean myBean can know what the session really is? I mean, methods of this EJB could have been invoked by a standalone client, RESTful WS or by some other view.
What should happen in such case? Should the annotation have no meaning, should it inject fresh MyBean instance for each request or maybe it should retain the same instance across all requests?

like image 474
Piotr Nowicki Avatar asked Nov 22 '11 22:11

Piotr Nowicki


1 Answers

Taken from the @SessionScoped specification

The session scope is active:

during the service() method of any servlet in the web application, during the doFilter() method of any servlet filter and when the container calls any HttpSessionListener, AsyncListener or ServletRequestListener.

So in short, yes. It is bound to the HttpSession. Also:

The session context is shared between all servlet requests that occur in the same HTTP session. The session context is destroyed when the HTTPSession times out, after all HttpSessionListeners have been called, and at the very end of any request in which invalidate() was called, after all filters and ServletRequestListeners have been called.

like image 172
Gonzalo Garcia Lasurtegui Avatar answered Oct 03 '22 22:10

Gonzalo Garcia Lasurtegui