Enterprise Beans

Contact Us

Enterprise JavaBeans (EJB)


You should use enterprise beans if your application has any of the following requirements:
  • The application must be scalable. To accommodate a growing number of users, you may need to distribute an application’s components across multiple machines. Not only can the enterprise beans of an application run on different machines, but also their location will remain transparent to the clients.
  • Transactions must ensure data integrity. Enterprise beans support transactions, the mechanisms that manage the concurrent access of shared objects.
  • The application will have a variety of clients. With only a few lines of code, remote clients can easily locate enterprise beans. These clients can be thin, various, and numerous.
Types of Enterprise Beans are
  • Session Beans
    1. Stateless Session Beans
    2. Stateful Session Beans
  • Entity Beans
    1. Bean Managed Persistent (BMP) Entity Beans
    2. Container Managed Persistent (CMP) Entity Beans
  • Message-Driven Bean

 Architecture of a J2EE Application

The following figure shows the architecture of a J2EE application:

 EJB Container

  • Persistence: EJB container manages the persistence of data for enterprise beans that are declared as Container Managed Persistence (CMP).
  • Transaction management: EJB container manages the transactions for different enterprise beans of an EJB application. A transaction is a set of operations that is executed as a single unit.
  • Security: EJB container provides security by allowing only authorized clients to access enterprise beans.
  • Life cycle management: EJB container manages the life cycle of an enterprise bean. An enterprise bean passes through various stages in its life cycle. EJB container is responsible for changing the state of an enterprise bean as per client calls. For example, the EJB container may create a new bean instance when a client request arrives or remove a bean instance from memory to release server resources.
  • Database connection pooling: EJB container creates a pool of database connections and allocates these connections to the enterprise bean as and when required. This service speeds up database access because the enterprise bean does not need to create a new database connection for each database access.
  • Remote client connectivity: EJB container allows clients at remote locations to access methods of an enterprise bean stored in the J2EE Server. EJB container provides this service using Remote Method Invocation (RMI).
  • The EJB container is also responsible for providing several Application Programming Interfaces (APIs) to support the running and deployment of an EJB. The various APIs provided by the EJB container are:
    • Java Platform, Standard Edition, (J2SE) API
    • EJB Standard Extension
    • JDBC Standard Extension
    • JNDI Standard Extension
    • JMS Standard Extension
    • JavaMail Standard Extension (for sending mail only)
    • JAXP
    • JTA Standard Extension (Only UserTransaction interface)
 EJB Architecture

The following figure shows various components of EJB architecture:
The various components of EJB architecture are:
  • EJB container: Provides the runtime environment for the EJB components. EJB container provides services, such as life cycle management, connection pooling, and security.
  • EJB component: Java classes that follow the EJB specification to implement the business logic of an application. EJB components are deployed in EJB container and are accessed using the remote and home interfaces of an enterprise bean.
  • EJB object: EJB container generates an EJB object, when a client needs to access an instance of the enterprise bean. The EJB object acts as an interface between the client and bean instances.
  • Home object: Implements location transparency that involves hiding the actual location of an EJB object from client. Home objects are generated by EJB container. Clients access EJB objects that are stored at different Java Virtual Machines (JVM), using the home object. The various tasks that clients can perform using home objects are:
    • Creating EJB objects
    • Searching for EJB objects
    • Removing EJB objects
 Types of Entity Beans

The two types of entity beans are:
  • Bean-Managed Persistence (BMP): This bean enables you to manage database queries and connectivity issues. You need to write the codes for establishing database connectivity and querying database tables in the bean class file.
  • Container-Managed Persistence (CMP): This bean enables EJB container to manage database queries and connectivity issues. In this type of bean, EJB container implicitly manages database connectivity.
The following figure shows the interaction between session beans and entity beans in an EJB application:

The following figure shows the use of message-driven beans in EJB applications:

 Click for Next Topic