Enterprise Java Beans (EJB)

An enterprise bean is a server-side component that encapsulates the business logic of an application.


EJB Container

An EJB container is a program that runs on the server and implements the EJB specifications.
The EJB container provides system-level services, such as transactions and security, to its enterprise beans. These services being transparent to application developer enable you to quickly build and deploy enterprise beans. The Container performs various tasks that include:

  • Transaction Management: EJB Container gives you access to transaction services, by exposing JTA (Java Transaction API) to control transactions. The container also allows for integration with other Transaction API's.
  • Security: EJB container provides both declarative and programmatic security. Integration with Lightweight Directory Access Protocol (LDAP) is possible using configuration.
  • Resource and Life Cycle Management: EJB container manages the resources like database connections, threads and sockets. Container creates, destroys, registers the bean objects and also activates and passivates them. The container is also capable of reusing them whenever required.
  • Remote Access: A client on the remote machine containing JVM can invoke an enterprise been running on the host machine. To support the remote accessibility the container uses the remote procedure call (RPC), referred to as Remote Method Invocation (RMI) in object oriented parlance.
  • Concurrent Access Control: Enterprise Java Beans using JPA control access to data using
    • Optimistic Locking
    • Pessimistic Locking
  • Clustering and load-balancing: Clustering is the process, where multiple computers work together to process information parallel and still appear as if the work has been done by a single computer. It also achieves load balancing through this process. Among many computers in cluster, the one with least load is selected and given information to process. Numerous algorithms exist for clustering and load-balancing and different vendors choose different algorithms to suit their purpose. EJB container has built-in features taking away the pain of clustering and load-balancing from application developer.

Benefits of Enterprise Beans

  • By leveraging system-level services to container, the bean developer only has to focus on solving business problem.
  • Since business logic is separate and can be accessed from various channels, this allows clients to be thinner. The presentation logic is completely decoupled from business logic.
  • Being portable components, EJBs can run on any J2EE complaint-servers.

When to use the Enterprise Beans

Some of the points are illustrated below that signify the use of enterprise beans.

  • Applications developed by using the enterprise beans deal with a variety of clients, simply by writing few lines of code, so that the client can locate the enterprise beans. The client locating the enterprise bean may be various, numerous and thin.
  • Enterprise beans support transaction to ensure the integrity of the database. Transaction is the mechanism of managing the concurrent access of shared objects.
  • Managing the fast growing number of users requires distributed application components across multiple machines means that the application must be scalable.