posted 19 years ago
In most of my interviews they usually ask "how do you decide when to use Stateless vs Stateful beans". Here is the tyical answer I give
Stateful Session Bean Considerations: Stateful session beans are appropriate if any of the following conditions are true:
1)The bean's state represents the interaction between the bean and a specific client.
2)The bean needs to hold information about, or on behalf of, the client user conversational state across method invocations.
3)The bean mediates between the client and the other components of the application, presenting a simplified view to the client.
4)Behind the scenes, the bean manages the work flow of several enterprise beans.
Because stateful session beans are private to a client, their demand on server resources increases as the number of users accessing an application increases. The beans remain in the container until they are explicitly removed by the client, or are removed by the container when they timeout.
The container needs to passivate stateful session beans to secondary storage as its cache fills up and the beans in the cache timeout. If the client subsequently accesses the bean, the container is responsible for activating the bean. This passivation/activation process imposes a performance overhead on the server.
Stateless Session Bean Considerations: You might choose a stateless session bean if any of these conditions exist:
1)The bean's state has no data for a specific client, that is, user conversational state does not have to be retained across method invocations on the bean.
2)In a single method invocation, the bean performs a generic task for all clients.
3)The bean fetches a set of read-only data (from a database) that is often used by clients. Such a bean, for example, could retrieve the table rows that represent the products that are on sale this month.
Use a stateless session bean to access data or perform transactional operations. Stateless session beans provide high scalability because a small number of such beans managed by the container in a stateless bean pool) can help serve a large number of clients. This is possible because stateless beans have no association with the clients. When a request for a service provided by a stateless session bean is received, the container is free to dispatch the request to any bean instance in the pool.
Real Context example: Let say in an ecommerce site. A stateful bean can be used to maintain the state of the user. From the point he adds the first item to the cart to the point of order completion. Stateless beans can be used in the middle for non-client specific actions like getting list of all items on promotional sale.
Sudhir V<br />(SCJP 1.2, SCWCD, OCA, SCBCD)