Library Zone Articles
External Articles
Byte Size

Discovery Zone Catalogue
Diary
Links
Bookstore
Interactive Zone Ask the Gurus
Discussion Groups
Newsletters
Feedback
Etc Cartoons
Humour
COMpetition
Advertising ASP Web Ring ASP Web Ring
iDevJobs.com - Jobs for Professional Developers
The Developer's Resource & Community Site
COM XML ASP Java & Misc. NEW: VS.NET
International This Week Forums Author Central Find a Job

EJB Session Beans

A session bean is created by a client and in most cases exists only for the duration of a single session. A session bean performs operations on behalf of the client such as database access or performing calculations. Although session beans can be transactional, they are not recoverable following a system crash. Session beans can be stateless or can maintain conversational state across methods and transactions. The container manages the conversational state of a session bean if it needs to be evicted from memory. A session bean must manage its own persistent data.

Each Session Bean is usually associated with one EJB client. Each Session Bean is created and destroyed by the particular EJB client that it is associated with. It is thus transient and will not outlive the virtual machine on which it was created. A Session Bean can either have states or they can be stateless. However, Session Beans do not survive a system shutdown.

There are two types of Session Beans. They are:

Stateless Session Beans: These types of session EJBs have no internal state. Since they are stateless, they need not be passivated and can be pooled in to service multiple clients.

Stateful Session Beans: These types of session EJBs possess internal states. Hence they need to handle Activation and Passivation. However, there can be only one stateful Session Bean per EJB client. Since they can be persisted, they are also called Persistent Session Beans. These types of EJBs can be saved and restored across client sessions. The getHandle() method returns a Bean object’s instance handle which can be used to save the Bean’s state. To restore a Bean from persistent storage, the getEJBObject() method is used.

The characteristics of a Session Bean can be summarized as follows:

  • Session EJBs execute on behalf of a single client. A Session Bean instance is an extension of the client that creates it.
  • Session beans may be transaction-aware.
  • They can update data in an underlying database.
  • They are relatively short-lived, since their lifetime is limited to that of their client.
  • They may be destroyed when the EJB server crashes. The client has to establish connection with a new Session Bean object to resume any computation.
  • Session beans do not represent data that has to be stored in a database.

Every Session Bean can read and update a database on behalf of the client. Its fields may contain conversational state on behalf of the client. The state describes the conversation represented by a specific client/instance pair. Within a transaction some of this data may be cached in the Bean.

Session beans are supposed to be private resources used only by the client that created the EJB Session Bean. For this reason a Session Bean hides its identity and is anonymous, in sharp contrast to an Entity Bean that exposes its identity using its primary key.

Typically, when developing a Session Bean, the steps that we normally follow are as follows:

  1. Define the EJB home interface
  2. Define the EJB remote interface
  3. Implement the factory object for this EJB
  4. Compile the EJB classes
  5. Declare the security and deployment properties.
  6. Generate the container classes using the tools suppiled by the EJB server/container vendor.
  7. Register the factory with the server
  8. Write the client code
  9. Compile the client code
  10. Start the client

What do you think of this article?

Have your say about the article. You can make your point about the article by mailing [email protected] (If you haven't allready joined, you can join by going to https://www.onelist.com/community/dev-java).

You can also write a review. We will publish the best ones here on this article. Send your review to [email protected]. Please include the title of the article you are reviewing.

Further Reading

The Enterprise JavaBeans Series:

Enterprise Java Beans By Gopalan Suresh Raj.
In this introduction to Enterprise Java Beans, Gopalan covers the bases then goes on to demonstrate how to build server side business object components. This article is the introduction to Gopalans series of Enterprise JavaBeans articles. (This series of articles is courtesy of Gopalan Suresh Raj)

Enterprise Java Beans Series - Components at the Server By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Model By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Naming Services and JNDI By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Transactions and JTS By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Lifecycle By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Servers By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Containers By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Components By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Entity Beans By Gopalan Suresh Raj.

Enterprise Java Beans Series - Writing an Entity Bean By Gopalan Suresh Raj.
Part 1 of a four part series: A four tier bank account example

Enterprise Java Beans Series - Writing a Session Bean By Gopalan Suresh Raj.
Part 2 of a four part series: A four tier bank account example

Enterprise Java Beans Series - Writing an EJB Client By Gopalan Suresh Raj.
Part 3 of a four part series: A four tier bank account example

Enterprise Java Beans Series - Writing an EJB Servlet Client By Gopalan Suresh Raj.
Part 4 of a four part series: A four tier bank account example


Author: Gopalan Suresh Raj

Gopalan has his own site at Author Central (visit him. He also maintains his own site at https://www.execpc.com/~gopalan/) - Contribute to iDevResource.com and you can have one too!

© Copyright 1997-2000 Gopalan Suresh Raj. Reproduced with Permission


iDevJobs.com - Jobs for Professional Developers

Contribute to IDR:

To contribute an article to IDR, a click here.

To contact us at IDevResource.com, use our feedback form, or email us.

To comment on the site contact our webmaster.

Promoted by CyberSavvy UK - website promotion experts

All content © Copyright 2000 IDevResource.com, Disclaimer notice

Join the Developers Webring



Code Project

Join the Developers Webring

Visit the IDR Bookstore!