Download PDF Home History |  | API Search Feedback FAQ

> ## What Is a Session Bean?

> A session bean represents a single client inside the Application Server. To access an application that is deployed on the server, the client invokes the session bean's methods. The session bean performs work for its client, shielding the client from complexity by executing business tasks inside the server.

> As its name suggests, a session bean is similar to an interactive session. A session bean is not shared; it can have only one client, in the same way that an interactive session can have only one user. Like an interactive session, a session bean is not persistent. (That is, its data is not saved to a database.) When the client terminates, its session bean appears to terminate and is no longer associated with the client.

> For code samples, see Chapter 25.

> ### State Management Modes

> There are two types of session beans: stateless and stateful.

> #### Stateless Session Beans

> A stateless session bean does not maintain a conversational state for the client. When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation. When the method is finished, the state is no longer retained. Except during method invocation, all instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client.

> Because stateless session beans can support multiple clients, they can offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients.

> At times, the EJB container may write a stateful session bean to secondary storage. However, stateless session beans are never written to secondary storage. Therefore, stateless beans may offer better performance than stateful beans.

> A stateless session bean can implement a Web service, but other types of enterprise beans cannot.

> #### Stateful Session Beans

> The state of an object consists of the values of its instance variables. In a stateful session bean, the instance variables represent the state of a unique client-bean session. Because the client interacts ("talks") with its bean, this state is often called the conversational state.

> The state is retained for the duration of the client-bean session. If the client removes the bean or terminates, the session ends and the state disappears. This transient nature of the state is not a problem, however, because when the conversation between the client and the bean ends there is no need to retain the state.

> ### When to Use Session Beans

> In general, you should use a session bean if the following circumstances hold:

> - At any given time, only one client has access to the bean instance.
> - The state of the bean is not persistent, existing only for a short period (perhaps a few hours).
> - The bean implements a Web service.

> Stateful session beans are appropriate if any of the following conditions are true:

> - The bean's state represents the interaction between the bean and a specific client.
> - The bean needs to hold information about the client across method invocations.
> - The bean mediates between the client and the other components of the application, presenting a simplified view to the client.
> - Behind the scenes, the bean manages the work flow of several enterprise beans. For an example, see the `AccountControllerBean` session bean in Chapter 36.

> To improve performance, you might choose a stateless session bean if it has any of these traits:

> - The bean's state has no data for a specific client.
> - In a single method invocation, the bean performs a generic task for all clients. For example, you might use a stateless session bean to send an email that confirms an online order.
> - The bean fetches from a database a set of read-only data 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.

Download PDF Home History |  | API Search Feedback FAQ

All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.