Basic EJB FAQ – Part 1

1)Who is EJB technology for?

EJB technology benefits a number of audiences:

Enterprise customers that build and/or deploy EJB-based applications – gain development productivity, can choose from a wide selection of EJB servers, create business logic that runs everywhere and is architecture independent, all this while protecting their existing IT investment!

ISVs and SIs that develop EJB components or applications based on EJB components – Invest in business logic that is widely deployable, across any OS and middleware, don’t need to choose one vendor-specific server platform. Like enterprise customers they also benefit from productivity gains and architecture independence

The EJB specification itself is mostly targeted at the EJB server vendors – It is the blueprint that instructs these vendors on how to build an EJB server that EJB components can execute on successfully

2)What are the design goals of the Enterprise JavaBeansTM architecture?

The Enterprise JavaBeans specification defines a standard architecture for implementing the business logic of multi-tier applications as reusable components. In addition to Enterprise JavaBeans components, the architecture defines three other entities: servers, containers, and clients. This architecture incorporates several design goals:

Enterprise JavaBeans servers are designed to wrap around legacy systems to provide fundamental services for containers and the components they contain.

Enterprise JavaBeans containers are designed to handle details of component life-cycle, transaction, and security management. By interceding between clients and components at the method call level, containers can manage transactions that propagate across calls and components, and even across containers running on different servers and different machines. This mechanism simplifies development of both component and clients.

Component developers are free to focus on business logic, since containers provide services automatically by interceding in component method calls. A simple set of callback interfaces are all that a developer needs to implement to participate in container provided services.

A client’s view of an Enterprise JavaBean remains the same regardless of the container it is deployed in. Any container in which an Enterprise JavaBean is deployed presents the same interfaces to the client. This extends to containers from different vendors, running against different servers and different databases, on diverse systems on a network. This client transparency ensures wide scalability for multi-tier applications.

Along with container managed transactions, the Enterprise JavaBeans architecture enables component- and client-managed transactions. Containers can participate in component or client initiated transactions to enforce transaction rules across method call and component boundaries. Components can also specify transaction types by method, enabling them to mix transaction types within a single object.

A variety of Enterprise JavaBean attributes, including the default component transaction type, can be specified at either development or deployment time, and enforced through mechanisms built into the container architecture.

The Enterprise JavaBeans architecture is based on the Java programming language, so enterprise Beans take full advantage of the “write once, run anywhereTM” standard.

3)What’s the client view of an Enterprise JavaBeans component?

The client view is provided through two interfaces — the home interface and the remote interface. These interfaces are provided by classes constructed by the container when a bean is deployed, based on information provided by the bean. The home interface provides methods for creating a bean instance, while the remote interface provides the business logic methods for the component. By implementing these interfaces, the container can intercede in client operations on a bean, and offers the client a simplified view of the component.

4)Why doesn’t the client interact with an Enterprise JavaBean directly?

To the client, there appears to be direct interaction with an Enterprise Java Bean through the home and remote interfaces. However, Enterprise JavaBeans architecture is designed to enable clients and components to exist in different runtimes on different systems on a network. The container intercedes between client and component, completely concealing both the bean instance and its own actions from the clients.

5)What methods are developers required to implement the Enterprise JavaBeans architecture?

There are three categories of Enterprise JavaBeans methods. First, the bean implements methods corresponding to those in its home interface — methods largely for creating, locating and accessing instances of the bean. Second, a bean implements business logic methods corresponding to those provided by its remote interface. Finally, a bean implements methods for interacting with the container. Since these methods aren’t intended for client access, they are hidden by the container.

6)What specific services does a container provide for an entity bean?

As with session beans, the tools for a container generate additional classes for an entity bean at deployment time to implement the home and remote interfaces. These classes enable the container to intercede in all client calls on the same entity bean. The container also generates the serializable Handle class, providing a way to identify the entity bean within a specific life cycle. These classes can be implemented to mix in container-specific code for performing customized operations and functionality. In addition to these custom classes, each container provides a class to provide metadata to the client. Finally, where specified by a particular bean, a container manages persistence of selected fields of the entity bean.

7)What’s the difference between container-managed and bean-managed persistence?

In container-managed persistence, entity bean data is automatically maintained by the container using a mechanism of its choosing. For example, a container implemented on top of an RDBMS may manage persistence by storing each bean’s data as a row in a table. Or, the container may use Java programming language serialization for persistence. When a bean chooses to have its persistence container managed, it specifies which of its fields are to be retained.

In bean-managed persistence, the bean is entirely responsible for storing and retrieving its instance data. The EntityBean interface provides methods for the container to notify an instance when it needs to store or retrieve its data.

8)How is an entity bean created?

An entity bean can be created in two ways: by direct action of the client in which a create method is called on the bean’s home interface, or by some other action that adds data to the database that the bean type represents. In fact, in an environment with legacy data, entity objects may “exist” before an Enterprise JavaBean is even deployed.

9)How does the client get a reference to an existing entity bean?

A client can get a reference to an existing entity bean in several ways:

Receiving the bean as a parameter in a method call

Looking the bean up through a finder method of the home interface

Obtaining the bean as a handle, a runtime specific identifier generated for a bean automatically by the container

10)How do you determine whether two entity beans are the same?

By invoking the EntityBean.isIdentical method. This method should be implemented by the entity bean developer to determine when two references are to the same object. Note that the equals and hashCode methods of Object are undefined for entity beans, since clients don’t directly access bean instances within a container.

11)How does a container manage access from multiple transactions on an entity bean?

Containers manage multiple transactions in one of two ways. First, the container can instantiate multiple instances of the bean and let the transaction management of the DBMS handle transaction processing issues. Or, the container can acquire an exclusive lock on the instance’s state in the database, and serialize access from multiple transactions to this instance.

12)How do enterprise beans handle concurrent and loopback calls on entity beans?

Concurrent calls in the same transaction context on the same Enterprise JavaBean component are illegal and may lead to unpredictable results. A bean can be marked as non-reentrant by its deployment descriptor. This allows the container to detect and prevent illegal concurrent calls from clients. On the other hand, some entity beans may require loopback calls: that is, calls where bean A is invoked, in turn invoking bean B, which then invokes a method call on bean A. This kind of concurrency is tricky and is best avoided.

Page Visitors: 369

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *