Chapter 4: Session Beans


A session bean is a business object, since it typically implements a business process. In practice, the functionality of a session bean can constitute, for example, setting up a balance sheet in a bookkeeping system, executing transfer of funds in banking software, or managing a materials supply depot.

Logically, a session bean is viewed as the server-side extension of the client program. A client has exclusive access via the EJB container to a particular instance of a session bean and can use its functionality. The notion of "client" extends also to other beans on a server and client programs on a client computer. For the implementation of its functionality a session bean can use other enterprise beans as well as the services that the EJB container and J2EE Application server provide. (See Figure 4-1.)

click to expand
Figure 4-1: Overview of a session bean.

When a client uses a session bean, it can execute various actions that lead to changes of state, that is, to changes in the data of enterprise beans. One distinguishes persistent and transient changes of state:

  • A persistent change of state remains over multiple sessions of the client. Altered data, for example, can be stored in a database. Session beans themselves do not have a persistent state, but use entity beans for the storage of persistent data (see Chapter 5, on entity beans) or, for example, access a database directly via JDBC.

  • A transient change of state lasts only for the duration of a session. The transient state thereby describes the state of the session of the conversation between client and server. Therefore, one speaks here also of a conversational state. The transient state is stored partly by the client and partly by the session bean. Therefore, the session bean can also be viewed as a logical extension of the client.

There are two types of session beans: stateless session beans and stateful session beans. In contrast to the stateful session beans, the stateless session beans do not have a conversational state. This means that no application method of a stateless session bean alters the state of a bean instance.

One must always keep the two types separate, since the EJB container treats them differently in managing bean instances. This affects the programming of methods for state management. These methods are called by the EJB container at run time to inform the bean instance about a state change. In the section on the life cycle of a session bean instance we shall go into the precise differences.

Methods of session beans can be executed in a transaction. This depends on whether the session bean uses explicit transactions or whether the EJB container is instructed by the configuration to guarantee a particular transactional behavior. A transaction guarantees that the corresponding method either is successfully executed or is made completely revocable. Thus there are no inconsistent states in the case of an error. Since the subject of transactions is rather complex, it will be dealt with in detail in Chapter 7.

Enterprise JavaBeans 2.1
Enterprise JavaBeans 2.1
ISBN: 1590590880
EAN: 2147483647
Year: 2006
Pages: 103 © 2008-2017.
If you may any questions please contact us: