0

I need help about Stateless and Stateful Session Beans. I have got a few questions.

  1. Please clearly say to me what is the difference between Stateless and Stateful Session Bean?
  2. A few people say that Stateful beans keep info about client. What kind of info does Stateful bean keep?
  3. Let's think that I have got a web project that contains EJB (Stateless and Stateful Session Beans). If I close the browser totally, can I reach same Stateful Session Bean instance again?

Thanks for your answers. :)

Ilkay Günel
  • 37
  • 1
  • 9

1 Answers1

0
  1. Stateless beans are totally interchangeable, any instance may service any incoming request, entirely at the container's discretion. For that reason, they should not store any state relating to a particular client or request, since they have no idea which client they'll be dealing with next. Stateful bean instances are created for a single client. They can store state and maintain it between calls, so that the client can continue a conversation rather than just discrete requests.
  2. The state which is stored can be whatever data you want. The key thing is that the same bean instance will be handling a single client's requests, so you can add whatever instance fields you need to store your data.
  3. The session will be identified by a browser cookie which the container will be correlating with a bean instance, so I'd expect that as long as the cookie hasn't been deleted or otherwise invalidated, it won't make any difference. (I'm less sure about this point, since it isn't in the strictly EJB part of the spec)
hugh
  • 2,237
  • 1
  • 12
  • 25
  • Thanks for this answer. I need to ask additional questions to you. If I don't use the @Remove notation in stateful session bean, does the bean be in server until I shut down server? – Ilkay Günel Aug 24 '15 at 10:16
  • No, the session bean will be removed whenever the server decides to remove it, it just won't call your method first. – hugh Aug 24 '15 at 12:42
  • Thanks again so much. Finally,to be sure, I wanna ask last question. If totally close the browser, I can not access the stateful session bean again. Is it true? Because I understood that when session closed, bean instance is destroyed. – Ilkay Günel Aug 24 '15 at 13:06
  • My expectation would be that the session is managed by a session cookie within your browser, which wouldn't (usually) be deleted just because the browser closes. If the session is inactive for a while, the stateful bean may be passivated, and if it's inactive a while longer it may be removed. (Exactly when is up to the container, and may be configurable in vendor-specific ways) – hugh Aug 24 '15 at 16:58