aName
aName

Reputation: 3053

EJB : understanding how container choose bean

I'm trying to understand how statefull beans works (I read the theoretical part and I know the difference between statfull and statelss beans ...), for that reason I created a statefull bean and a rest API to access it.
I find out that the container create/instantiate a new bean for every request.
then I used a servlet to access the same statfull bean, and this time the container crate just one bean that serves all requests.
So my questions are :

  1. why does the container create many bean for rest API ?? I know that it consider each request as a separate client but how it knows, since rest API or servlet are accessed using http requests??
  2. why it consider request when it comes from servlet as one client?? (therefor it create one bean)
  3. in my case (doing test localy) how to force the container to create more beans (how to simulate more than one client) when using servlet.

Thank you in advance

Upvotes: 1

Views: 56

Answers (1)

Frito
Frito

Reputation: 446

I checked the specs, but I could not find something about this. But that seems reasonable:

Somebody must take care about the SFSB instance, closing it when done.

When exposing an EJB business method of a SFSB as REST service, a generic servlet is used. The only scope available is the request scope of a single (stateless) HTTP call, so after the call is done, the generic servlet should close the SFSB.

The servlet has an explicit lifecycle. An injected EJB is create during initialisation of the servlet and can be closed on destroy.

You can lookup new SFSB instances with every HTTP session created, using the session context for subsequent calls on this session and closing the SFSB when the matching session is closed.

Upvotes: 1

Related Questions