s4194313
s4194313

Reputation: 39

relationship between CDI , Stateful session bean, stateless session bean ,pojo and different scopes

Im learning j2ee , pardon me if questions appear very basic.

In httpsession session ID is stored in client side and the data associated with it is stored in server side.

When stateful session bean interacts with web client a browser makes an initial request to a Java EE web application it gets a JSESSIONID that the server can associate with a specific HTTPSession instance. By holding on to this JSESSIONID, the browser can provide it with each follow-up request and this will activate the same http session server-side. Ref: Using a Stateful Session Bean to track an user's session

Now when I use CDI @SessionScoped on sfb does that mean just JSESSIONID will be returned for that SFB or EJB container (?) will store another copy of sfb on server side ? (session.setAttribute(SFB-Another-Reference) )

Now when I use CDI @RequestScoped on sfb is it useless as SFB lives till session?

Now when I use CDI @SessionScoped on slb is it useless to use @SessionScoped since slb live only for method invocation ?

Now when I use CDI @SessionScoped on POJO Does that mean the EJB container (?) store the pojo in session. (session.setAttribute(POJO))

Can CDI differentiate between SFB ,SLB and a POJO ?

Upvotes: 1

Views: 1097

Answers (1)

Gas
Gas

Reputation: 18050

Now when I use CDI @SessionScoped on sfb does that mean just JSESSIONID will be returned for that SFB or EJB container (?) will store another copy of sfb on server side ?

JSESSIONID is related to the http session, not to any EJB (Statless nor Stateful). When you use @SessionScoped your sfb will be associated by CDI with you http session.

Now when I use CDI @RequestScoped on sfb is it useless as SFB lives till session?

No, it is useless from other reason, as new stateful bean will be created on each request, which in effect breaks the whole idea of stateful bean, which should store state between requests.

Now when I use CDI @SessionScoped on slb is it useless to use @SessionScoped since slb live only for method invocation ?

More or less yes. Some CDI frameworks (OWB for example) won't even allow that, and only allows @Dependent scope. SLB actually lives longer, but since it doesn't store any state its instances can be pooled and used by many clients.

Now when I use CDI @SessionScoped on POJO Does that mean the EJB container (?) store the pojo in session. (session.setAttribute(POJO))

That POJO is associated by the CDI framework with http session, it is not directly stored in session as attribute.

Can CDI differentiate between SFB ,SLB and a POJO ?

Yes, CDI container can differentiate these and treats them differently e.g. types visibility, allowed scopes. For example fragment of the spec:

A stateless session bean must belong to the @Dependent pseudo-scope. A singleton bean must belong to either the @ApplicationScoped scope or to the @Dependent pseudo-scope. If a session bean specifies an illegal scope, the container automatically detects the problem and treats it as a definition error. A stateful session bean may have any scope.

Upvotes: 1

Related Questions