I am new to ejbs. I wrote a stateful session bean scoped @SessionScoped. Then I injected the ejb into my servlet.
@Local
@SessionScoped
@Statueful
public class CartServiceImpl implements CartService {
private int i = 0;
public int getI() {
return i++;
}
}
In my servlet
@Inject
private CartService cartService;
.
.
.
out.print(cartService.getI());
Then I opened two browsers (IE, FF) and have hit the servlet. In IE I see output starting from 0 to n. In firefox also I see the output starting from 0 to n.
Then I created created an ear which has a jar and a war. The jar contains all ejbs. war contains the servlets.
This is how I injected the ejb into the servlet
@Resource(lookup = "java:app/ejb-beginner-ejb/CartServiceImpl")
private CartService cartService;
Then I tried requesting the same servlet from IE and FF and I get unexpected output.
The output is as follows
In IE I requested for first time and I get 0 as output. Then I refreshed the page and I get 1 as output. Then I move to FF, send the request for first time and I get 2 as output instead of 0. Then I move to IE and refresh the page and I get 3 as output instead of 2.
What I understood is the app server is creating only one instance of the stateful ejb. How can I fix this?
What is the difference between packaging the ejbs in the war and packaging them separately in a jar module?