0

From what I understand @Stateless means that the state of every encountering of a client to a server is starting from scratch, and @Stateful means that the server saves in his memory the client's data.
("stateless means there is no memory of the past. Every transaction is performed as if it were being done for the very first time.
statefull means that there is memory of the past. Previous transactions are remembered and may affect the current transaction.
").

I have been reading http://www.tutorialspoint.com/ejb/ejb_stateless_beans.htm and http://www.tutorialspoint.com/ejb/ejb_stateful_beans.htm.
The examples there show that in a @Stateless annotation, when the client exits and re-enters, it seems as if the server did save the data and presented the books that were added from the "previous" client, but in the @Stateful annotation it seems as if the server treated the returning client as a new one, and didn't save the list the client created.

I assume my misunderstanding has to do with misunderstanding of the terms "state", "memory" or "transaction", but at the moment, I am confused as the definition seems contradicting to the outputs.

Achi Even-dar
  • 427
  • 1
  • 10
  • 20

1 Answers1

0

You have a good understanding of the difference between stateful and stateless session beans. You just happened to read a confusing example.

Stateless

Stateless session beans are managed by the EJB container that spawns a pool of instances. You can have instance variables in them but you cannot rely on it because the server does not guarantee that you will hit the same instance between method calls.

In the stateless example, adding books to the bookShelf list is not a very good idea because the second lookup may return a different bean from the instance pool and it is possible to not find the book added in the first step.

It's explained at the bottom of the article :

Output shown above may vary depending upon how many stateless ejb object JBoss is maintaining.

In case a single stateless ejb object is maintained, you may see the same list of books after each lookup.

EJB Container may return same stateless ejb object for every lookup.

Stateless ejb bean is keeping value of instance variable till the server is not restarted.

The author understands how stateless session beans work, but the use case is confusing with too many "may". It is not recommended to store instance variables in stateless session beans since you have no guarantee on which instance you will get in the pool.

Stateful

On the other hand, stateful session beans will be bound to a client after a lookup. The client will keep hitting the same instance in the EJB container as long as it keeps the reference to the instance it got after the lookup. The last part is important because if the client does another lookup, it will get another instance of stateful session bean.

In the second example, after adding a book and displaying how many we have, it's normal to have 1. The second lookup will give you another instance, hence the bookShelf is empty.

Stateful session beans are useful to store state on the server side, just be careful to keep instance reference on the client's side.

Community
  • 1
  • 1
bobuns
  • 277
  • 1
  • 4
  • 12
  • Ok, let's simplify this by ignoring the random Stateless annotation, and focus on the Stateful, since the output, from what I understand, should be stable. (Please correct me if I'm wrong). Why when the client re-enters and does lookup, he gets an empty shelf? That is what I don't understand, if the server saves his state, shouldn't it mean that the lookup should return the Database the client created? And if you could explain the meaning of "keeping reference to the instance". Thank you – Achi Even-dar Apr 02 '15 at 09:36
  • When you do a lookup, there is no mechanism for the server to know that the client is the same. The client has to keep the same reference obtained with the first lookup itself. A typical use case is the cart on a shopping website. The reference to the stateful session bean should be kept in user's session to reuse it between changes in the cart. – bobuns Apr 07 '15 at 19:08