Programming Tutorials

EJB Entity Beans

By: Priya in EJB Tutorials on 2007-10-07  

EJB is a Java API specification that provides a component architecture for the development and deployment of distributed business objects, but many Java programmers use EJB only for its database access and Java object-persistence capabilities. If your application is highly transactional, requires high availability, and is likely to have many concurrent users, you might want to consider using EJB.

EJB is a standard part of the Java EE and is therefore supported by all Java EE compliant application servers. Application servers that support EJB do so by providing an EJB container that hosts EJB components, just as a servlet container hosts servlets and JSPs. And just as servlet containers may be clustered to provide load balancing and fault tolerance for servlets, EJB containers can be clustered to provide the same functionality for distributed business objects. An EJB container can support three types of components: entity beans, session beans, and message beans. We won't discuss session and message beans because they aren't persistent. We'll focus on entity beans, which can be persisted to a data store by using one of the following two mechanisms:

  • Container-managed persistence (CMP): You provide a mapping that specifies how to map fields from your entity bean objects to fields in a database, and the EJB container
    manages the persistence of your entity beans in the database.
  • Bean-managed persistence (BMP): You implement the persistence of your objects by using JDBC, an O/R framework, JDO, or some other technology. The EJB container will notify your code when it's time to store or retrieve an object to or from the database.

The benefits provided by using EJB for persistence are many; here are some of the most significant:

  • Built-in O/R framework: If you use EJB CMP, you get all the benefits of using an O/R framework plus the added benefits of EJB. However, the O/R mapping capabilities of
    CMP don't address some of the harder tasks in O/R mapping such as optimistic locking, batch updates, and so on.
  • Scalability and high availability: EJB containers can be clustered to allow your application to scale up to meet the needs of more and more users. If an EJB container fails, the objects that were running in that container will automatically and transparently failover to continue execution in another EJB container.
  • Declarative transaction support: EJB allows you to declare the transactional characteristics of your business objects. Instead of writing the code to begin and end transactions, you simply declare the transactional requirements of each of your object's methods and let the EJB container do the rest. If you have a complex and highly transactional system, this is an important benefit.
  • Declarative method-level security: EJB allows you to declare the security characteristics of your business objects. Instead of writing code to ensure that only certain users working in certain roles can use your objects, you simply declare the security constraints of each of your object's methods and let the EJB container handle the security.
  • Distributed object support: EJB is designed to support the development and deployment of distributed business objects that are callable via Java Remote Method
    rotocol (IIOP).

One disadvantage of EJB is complexity. The technology is complex, and the learning curve is steep. To learn EJB development, you need to learn the EJB philosophy, the EJB API, recommended EJB patterns, EJB development tools, and the EJB deployment descriptors. To learn EJB deployment, you need to learn how to use the administration and deployment tools provided by the various Java EE application servers that you intend to support, each of which can vary quite significantly.

Another disadvantage of EJB is development overhead. When EJBs were first introduced, you needed to create at least three (often four) files for a single EJB. Tools such as XDoclet made the process easier, but until recently there was a lot of complexity in developing even simple beans. EJB 3.0, part of the latest Java Platform, Enterprise Edition 5 specification, has made steps to make the writing process easier, but there are still many issues to understand and consider. There is also considerable runtime overhead associated with using entity beans as the container interposes a variety of services for your beans. EJB does a lot for you, but with increased complexity and significant development overhead. Make sure that you really need the benefits provided by EJB before you commit to using it in your application.

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in EJB )

Latest Articles (in EJB)