October 28, 2020
Hot Topics:

EJB 3 Session Beans

  • By Raghu R. Kodali, Jonathan R. Wetherbee, and Peter Zadrozny
  • Send Email »
  • More Articles »

This article will discuss EJB 3 session beans, the core business service objects used by EJB client applications. You'll gain an understanding of the new and simplified EJB 3 session bean model, with insight into the following topics:

  • Types of session beans, both stateful and stateless, and when to use which
  • The bean class, business interfaces, and business methods
  • Callback methods
  • Interceptors
  • Exception handling
  • Client view
  • Dependency injection with annotations related to session beans

Introduction to Session Beans

Session beans are Java components that run in either stand-alone EJB containers or EJB containers that are part of standard Java Platform, Enterprise Edition (Java EE) application servers. These Java components are typically used to model a particular user task or use case, such as entering customer information or implementing a process that maintains a conversation state with a client application. Session beans can hold the business logic for many types of applications, such as human resources, order entry, and expense reporting applications.

Types of Session Beans

Session beans are of two types, as follows:

  • Stateless: This type of bean does not maintain any conversational state on behalf of a client application.
  • Stateful: This type of bean maintains state, and a particular instance of the bean is associated with a specific client request. Stateful beans can be seen as extensions to client programs that are running on the server.

You will drill down into more specifics of stateless and stateful beans in the following sections.

When Do You Use Session Beans?

Session beans are used to write business logic, maintain a conversation state for the client, and model back-end processes or user tasks that perform one or more business operations. Typical examples include the following:

  • A session bean in a human resources application that creates a new employee and assigns the employee to a particular department
  • A session bean in an expense reporting application that creates a new expense report
  • A session bean in an order entry application that creates a new order for a particular customer
  • A session bean that manages the contents of a shopping cart in an e-commerce application
  • A session bean that leverages transaction services in an EJB 3 container (removing the need for an application developer to write the transaction support)
  • A session bean used to address deployment requirements when the client applications are not colocated on the same server
  • A session bean that leverages the security support provided by the container on the component or method level

Session beans can be used in traditional 2-tier or 3-tier architectures with professional/ rich client applications, or in 3-tier web-based applications. These applications can be deployed in different logical and physical tier combinations. In the next section, you will investigate some of the possible combinations.

3-Tier Architecture with Rich Client

Figure 1 shows a typical architecture for a session bean in 3 tiers, with a rich client front-end application that has some data entry screens used by end users like customer service representatives, bank tellers, and so on. These client applications can be developed using Java Swing technology with the Java Platform, Standard Edition (Java SE), or they can be plain old Java objects (POJOs) that are run from the command line. Generally, the end user launches the client application from his desktop, enters some data, and triggers an event by pressing some user interface component such as a Submit button. The general workflow may look something like this:

  1. User action establishes a connection to the session bean running in the EJB container using remote method invocation (RMI).
  2. The client application invokes one or more business methods in the session bean.
  3. The session bean processes the request and validates data—by interacting with databases, enterprise applications, legacy systems, and so on—to perform a certain business operation or task.
  4. The session bean finally sends a response back to the client application, either through data collections or simple objects that contain acknowledgment messages.

Click here for a larger image.

Figure 1. Session beans in a 3-tier architecture

3-Tier Architecture for a Web Application

This architecture, as shown in Figure 2, is typically front-ended by a web application running in the browser of a desktop or laptop machine. These days, other types of client devices, such as PDAs, cell phones, and telnet devices, are also being used to run these applications. The web application running in a browser or mobile device renders the user interface (data entry screens, Submit buttons, etc.) using web technologies such as JavaServer Pages (JSP), JavaServer Faces (JSF), or Java Servlets. Typical user actions, such as entering search criteria or adding certain items to the web application shopping cart, will invoke/call session beans running in an EJB container via one of the aforementioned web technologies. Once the session bean gets invoked, it processes the request and sends a response back to the web application, which formats the response as required, and then sends the response on to the requesting client device (browser, PDA, telnet, etc.).

Click here for a larger image.

Figure 2. Session beans in a 3-tier architecture with a web application

In the 3-tier architecture just discussed, the client application (which is the web application) and the session beans can be run within the same instance of an application server (colocated) or from different instances running on the same machine. They can also be run in physically separate machines that have an instance of an application server.

Stateless Session Beans

Stateless session beans are comprised of the following elements:

  • Business interfaces, which contain the declaration of business methods that are going to be visible to client applications
  • A bean class, which contains the business method implementation to be executed

The Bean Class

A stateless session bean class is any standard Java class that has a class-level annotation of @Stateless. If deployment descriptors are used instead of annotations, then the bean class should be denoted as a stateless session bean. If you use both annotations and deployment descriptors (mixed mode), then the @Stateless annotation must be specified if any other class-level or member-level annotations are specified in the bean class. If both annotations and deployment descriptors are used, then the settings or values in the deployment descriptor will override the annotations in the classes during the deployment process.

To illustrate the use of stateless session beans, you will create a SearchFacade session bean that provides various search facilities to client applications regarding available wines. The workflow is as follows:

  1. Users of the application will type in or choose one or more search criteria, which will be submitted to the SearchFacade session bean.
  2. The SearchFacade bean will access back-end databases to retrieve the requested information. To simplify the code examples in this , you will actually retrieve the list of hard-coded values within the bean class.
  3. The bean returns to the client applications the information that satisfied the search criteria.

Listing 1 shows the definition of the SearchFacade bean. In the following sections of this article, you will build the code that will show the preceding workflow in action. SearchFacadeBean is a standard Java class with a class-level annotation of @Stateless.

Listing 1. SearchFacadeBean.java

package com.apress.ejb3.chapter02;

import javax.ejb.Stateless;


public class SearchFacadeBean implements SearchFacade,
   SearchFacadeLocal {
   public SearchFacadeBean() {

Page 1 of 5

This article was originally published on December 22, 2006

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date