Domanda

I am new to ejbs and cdi. To my understanding a stateful ejb stores the data in the instance variables and destroys the stateful ejb after the request is finished.

I recently attended an interview where the interviewer asked me what kind of ejb would I use in an online shopping kind of application.

If I have to do it without ejbs, I create a HttpSession and then add the user interest in the session and then show him another page to continue or make the payment or exit.

If I want to accomplish the same using stateful ejbs, I dont understand why should I use stateful ejbs, what is its significance? Because once the request is completed the ejb is destroyed and the user interest/cart-details are destroyed.

Secondly what I am not able to understand about cdi is, suppose I am injecting the service class into my servlet, because injection happens only once there will be only one instance of the service class. When more than two requests come the instance variables of the stateful ejb get corrupted. So I guess when I am using stateful ejbs I have to use @RequestScoped annotation. Am I right?

È stato utile?

Soluzione

Stateful session bean will allow you to store the same state as an http session. Few advantages over using http session that I can think of:

  1. Scalability - Your SFSB can be deployed on another server and scale independently using remote interfaces
  2. Non-web clients - You can use SFSB to maintain state for a non-web client where http session will not be available
  3. The other benefits that come with using an EJB

To hold a reference to a stateful EJB in a servlet you should use @SessionScoped with @Inject as indicated in this answer

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top