First you need to understand how beans are created and processed on the server.
For a stateless session with beans, the server can support a variable number of instances in the pool. Each time a client requests such a stand-alone bean (for example, through a method), a random instance is selected to execute this request. This means that if the client makes two subsequent requests, it is possible that two requests for an instance of the idle bean serve the requests. There is actually no dialog state between the two requests. Also, if the client disappears, the idle bean is not destroyed and can serve the next request from another client.
A bean session, on the other hand, is closely related to the client. Each instance is created and attached to one client and serves only requests from this particular client. It so happened that if you execute two subsequent requests for a stateful bean, your request will always be served from the same bean instance. This means that you can maintain a dialog state between requests. At the end of the life cycle, the client calls the remove method, and the bean is destroyed / ready to collect garbage.
When to use statelessness or condition?
This mainly depends on whether you want to maintain an interactive state . For example, if you have a method that adds numbers and returns the result, you are using a bean, because it is a one-time operation. If you call this method a second time with different numbers, you are no longer interested in the result of the previous addition.
But if you want, for example, to count the number of requests made by the client, you should use a stateful bean. In this scenario, it is important to know how often the client requested the bean method before, so you need to maintain the dialog state in the bean (for example, with a variable). If you use a stand-alone bean, here the client request will be served each time from another bean, which will interfere with your results.
tobiasdenzler Sep 20 '13 at 12:47 2013-09-20 12:47
source share