When to use a bean state session through a bean inactivity session?

A bean state session is defined as follows:

Beans state session The state of an object consists of the values โ€‹โ€‹of its instance variables. In a session with a bean state, instance variables represent the state of a unique client-bean session. Because the client interacts (โ€œnegotiations") with its bean, this state is often called an interactive state.

The idle bean session is defined as follows:

Standing Beans Session A non-bean session does not support interactive state with the client. When a client invokes methods of non-existent beans, instance variables of a bean may contain a state specific to that client, but only for a while. When the method is completed, the client-specific state should not be saved. However, clients can change the state of instance variables in a federated state without a beans state, and this state persists until the next stateless bean call. Except during method invocation, all instances of the idle bean are equivalent, allowing the EJB container to assign an instance to any client. That is, a session state without a bean should be applied across all clients.

The advantage of a non-bean session is also mentioned as follows:

Because a Beans stateless session can support multiple clients, they can offer better scalability for applications requiring large numbers of clients. Typically, an application requires less session without saving Beans state than a session with Beans state to support the same number of clients.

So, the question is, when do I need to use a session with beans state? To my naive understanding of this issue, you need to stick to a bean session bean as it can.

What are the candidates in which to use a session with a bean state? Any good examples?

Bean Session

+64
ejb stateful-session-bean session-bean stateless-session-bean
Aug 01 '13 at 17:13
source share
2 answers

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.

+107
Sep 20 '13 at 12:47
source share
โ€” -

I think that the best example of using the Stateful bean session for the Cart is where you store all the products that the user wants to buy.

+30
Jun 24 '14 at 5:13
source share



All Articles