Article

Enterprise JavaBeans: A Primer

Page: 1 2 3 4 Next

Session Beans

A session bean represents a single client and is not shared across clients. A client invokes the session bean's methods, which are directed through the EJB container to the enterprise bean. The session bean performs the business logic for the client and the container returns control to the client. A session bean is not persisted across multiple sessions. There are two types of session beans: stateful and stateless.

Stateful Session Beans

A stateful session bean maintains a conversational state with one client for the duration of a single session. This implies that the stateful session bean can maintain instance variables across multiple invocations from one client during a single session.

Once the client finishes interacting with the enterprise bean, and the EJB container removes the enterprise bean, the session for the bean ends and all state data for the bean is discarded.

Stateless Session Beans

A stateless session bean does not maintain a conversational state for each individual client. Each invocation of a stateless session bean should be considered as a request to a brand new object instance, since any instance-variable state will be lost between invocations.

Stateless session beans are not persisted to secondary storage by the EJB container; therefore a programmer must recognize that all data is transient between invocations for each client. The transient nature of stateless session beans allows an EJB container to reuse bean instances and therefore, usually optimize the performance of the beans.

Entity Beans

An entity bean is intended to represent the business logic for an entity existing in persistent storage. Entity beans share some of the same qualities that you would find in a relational database, for example:

  • Entity beans are persistent - An entity bean's state exists beyond the lifetime of the application in which it is created, or for that matter, the lifetime of the EJB container. This implies that the entity bean can be restored to its original state by the EJB container.
  • Entity beans allow shared access – they may be shared by multiple clients. The concurrency is handled by the container.
  • Entity beans have primary keys – primary-key classes exist to identify an instance of an entity bean. The primary key contains all the information needed to find a persistent entity.
  • Entity beans may participate in relationships – local interfaces have been introduced to manage relationships between beans.
  • Entity beans can participate in transactions – as data can be accessed and changed by multiple clients, it is important for entity beans to be able to specify the transactional properties for their interaction. Transaction properties are specified declaratively in deployment descriptors; transaction boundaries are handled by the container.

The object-relational mapping implied by entity beans requires that an entity bean be responsible for inserting, updating, selecting, and removing data within the data source. This process of managing the communication between the component and the data source is called persistence. In other words, persistence is this process of writing the information to an external data source. There are two types of persistence for entity beans: bean-managed persistence (BMP) and container-managed persistence (CMP). We discuss these in the next section.

Bean-Managed Persistence (BMP)

With bean-managed persistence (BMP), the programmer is responsible to write all of the code within the entity bean to access the data source. BMP allows more flexibility to the programmer because all access to the data source is controlled by the programmer.

Container-Managed Persistence (CMP)

With container-managed persistence the EJB container handles all database access required by the entity bean. As a result, the bean's data-access code is not coupled to a specific data source. This frees the programmer from writing any of the data-access code and allows the entity bean to be deployed in different containers and/or against different data-sources.

Session Bean and Entity Bean Relationships

Figure 1 illustrates the relationships that exist between a client, an EJB container, session beans, and entity beans:

1247_ejbArch
Figure 1

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links