An Application Server Model - The Enterprise Java Beans
EJB Overview
Enterprise Java Beans (EJB) is a technology specification from Sun Microsystems
Inc. that specifies a framework for building component-based distributed
applications. As an application server framework, the EJB servers address
transaction processing, resource pooling, security, threading, persistence,
remote access, life cycle etc.
The EJB framework specifies construction, deployment and invocation
of components called as enterprise beans. The EJB specification classifies
enterprise beans into two categories: entity beans and session beans. While
entity beans abstract persistent domain data, session beans provide for
session specific application logic. Both types of beans are maintained
by EJB compliant servers in what are called as containers. A container
provides the run time environment for an enterprise bean. Figure 4 shows
a simplified architecture of transaction management in EJB compliant application
servers.
Figure 4 - EJB and Transactions
An enterprise bean is specified by two interfaces: the home interface and the
remote interface. The home interface specifies how a bean can created or found.
With the help of this interface, a client or another bean can obtain a reference
to a bean residing in a container on an EJB server. The remote interface specifies
application specific methods that are relevant to entity or session beans.
Clients obtain references to home interfaces of enterprise beans via the Java
Naming and Directory Interface (JNDI) mechanism. An EJB server should provide
a JNDI implementation for any naming and directory server. Using this reference
to the home interface, a client can obtain a reference to the remote interface.
The client can then access methods specified in the remote interface. The EJB
specification specifies the Java Remote Method Invocation (RMI) as the application
level protocol for remote method invocation. However, an implementation can
use IIOP as the wire-level protocol.
In Figure 5, the client first obtains a reference to the home interface, and
then a reference to an instance of Bean A via the home interface. The same procedure
is applicable for instance of Bean A to obtain a reference and invoke methods
on an instance of Bean B.
EJB Transaction Model
The EJB framework does not specify any specific transaction service (such
as the JTS) or protocol for transaction management. However, the specification
requires that the javax.transaction.UserTransaction interface of the JTS
be exposed to enterprise beans. This interface is required for programmatic
transaction demarcation as discussed in the next section.
The EJB framework allows both programmatic and declarative demarcation of transactions.
Declarative demarcation is needed for all enterprise beans deployed on the EJB.
In addition, EJB clients can also initiative and end transactions programmatically.
The container performs automatic demarcation depending on the transaction
attributes specified at the time of deploying an enterprise bean in a container.
The following attributes determine how transactions are created.
-
NotSupported: The container invokes the bean without a global transaction
context.
-
Required: The container invokes the bean within a global transaction
context. If the invoking thread already has a transaction context associated,
the container invokes the bean in the same context. Otherwise, the container
creates a new transaction and invokes the bean within the transaction context.
-
Supports: The bean is transaction-ready. If the client invokes the
bean within a transaction, the bean is also invoked within the same transaction.
Otherwise, the bean is invoked without a transaction context.
-
RequiresNew: The container invokes the bean within a new transaction
irrespective of whether the client is associated with a transaction or
not.
-
Mandatory: The container must invoke the bean within a transaction.
The caller should always start a transaction before invoking any method
on the bean.
Transaction Demarcation
The EJB framework supports three types of transaction demarcation.
-
Declarative Demarcation: This is also called as container managed
demarcation. The container demarcates transactions on behalf of the
bean. The Required or RequiresNew attribute is specified in a deployment
descriptor at the time of deploying the bean on an EJB server. The bean
can use the javax.ejb.EJBContext.setRollbackOnly() method to mark the transaction
for rollback.
-
Bean Managed Demarcation: This is similar to the client-managed
demarcation.
-
Client Managed Demarcation: Java clients can use the javax.transaction.UserTransaction
interface to demarcate transactions programmatically.
Resource Enlistment
Resource enlistment is automatic with EJB. The EJB containers automatically
enlists connections to EJB-aware resource managers whenever a bean obtains
a connection.
Application Synchronization
The EJB specification provides the javax.ejb.SessionSynchronization interface
for application synchronization. When implemented by a bean, the container
calls the afterBegin, beforeCompletion and afterCompletion methods for
application synchronization during the two-phase commit process.