Implementing EJB 3.0 Session Beans

From Wiki

Jump to: navigation, search

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. A session bean is not shared; it can have only one client (as well as an interactive session can have only one user). 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.

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

Contents

Create session beans

Essential tasks

Add life-cycle event handlers

Package and deploy session beans

Create a session bean client

When to Use Session Beans

  • 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:

  • 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.

Choose a stateless session bean if:

  • The bean's state has no data for a specific client.
  • The bean performs a generic task for all clients. (For example, 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. (The bean retrieve the table rows that represent the available rooms in an accommodation).
Personal tools