What is EJB?
By Brad Warren, OCI Software Engineer
Enterprise Java Beans (EJB) is a specification provided by Sun Microsystems that defines a framework and development model for server side components in Java.
Much like the Java platform itself, EJB is simply a specification; the implementation is provided by vendors who develop their products to conform to the specification.
EJB is built on top of existing distributed computing technologies, such as CORBA and RMI, and it's designed to simplify software development by eliminating a lot of the infrastructure development associated with storing, retrieving, and accessing server side components. It is also designed to provide one common interface to all infrastructure services, such as naming and transaction processing.
To date, many application server vendors have provided EJB implementations as part of their products. As Java implementations, EJB components may be run on any software platform and, in addition, may be run on any EJB compliant application server.
EJB components are called beans. These beans are self-contained, reusable objects that execute within the context of a container. The container is responsible for providing management and control for the beans running inside of it. It manages client connections, bean lifecycle, state management, storage and persistence, security, and transactions.
Beans can also be customized by the developer.
The EJB server provides an implementation of the container and at runtime may support multiple containers. EJB servers can be transaction-processing (TP) monitors, application servers, or database servers.
Types of Beans
Enterprise Java beans can take the form of a session bean or an entity bean.
A session bean is created by a client and usually executes operations on the client's behalf.
A stateless session bean may only exist for the duration of a single method call; however most implementations will maintain the stateless session bean for the duration of an entire client session.
A stateful session bean will exist for multiple method calls, maintaining the bean's state in between calls. A stateful session bean is responsible for maintaining its own persistent data.
An entity bean is a representation of a persistent data object. Normally, this means that the entity bean corresponds to some data stored in a database.
An entity bean may have its persistence managed by the container or by itself.
If the bean's persistence is managed by the container, the container provides the mapping between the bean's data and the data store.
When the bean manages its own persistence, it must provide the mechanism for storing and retrieving its data from the data store (usually JDBC).
For identification, the entity bean has a primary key defined. This primary key may be used to store and retrieve the bean data, as well as uniquely identify the entity bean within the container.
Transactions for the beans is based on the Java Transaction Service (JTS).
The JTS provides an interface to the Object Transaction Service (OTS) for CORBA, which supports transactions across multiple objects and multiple databases.
Applications communicate with JTS through the Java Transaction API (JTA), which provides access to transactions.
Transactions for the beans may be managed by the container, the client, or the beans themselves.
If the client or the beans manage transactions, they must explicitly start, join, commit, and rollback transactions manually through the JTA. Otherwise, no transaction demarcation code is necessary.
The following sources have more information about EJB:
-  Monson-Haefel, Richard, Monson-Haefel, Robert, Loukides, Mike. Enterprise Java Beans. O'Reilly & Associates. ISBN 1565926056.
-  Valesky, Thomas C. Enterprise Java Beans: Developing Component-Based Distributed Applications. Addison Wesley. ISBN 0201604469.
-  Vogel, Andreas and Madhavan Rangarao. Programming with Enterprise Java Beans, JTS and OTS. John Wiley & Sons. ISBN 0471319724.
Software Engineering Tech Trends (SETT) is a regular publication featuring emerging trends in software engineering.