CONTENTS | PREV | NEXT | INDEXJ2EE BluePrints



Questions and Answers - Enterprise JavaBeans Tier

  1. How can I find a particular enterprise bean (component based on EJB technology)?

  2. Should an enterprise bean look up the Java Database ConnectivityTM (JDBCTM) technology-based drivers directly from the ejbCreate() method?

  3. What are some common pitfalls and practices to avoid when using enterprise beans?

  4. How can I take advantage of my application server's connection pooling features?/li>
  5. How can I hold and pass references to enterprise beans?

  6. How can nested transactions be implemented with enterprise beans?

  7. Can I use synchronization primitives in my enterprise beans?

  8. How do I map a single entity bean to multiple tables?

  9. What does "stateless" mean in a stateless session bean? Can stateless session beans cache any information?

  10. What are enterprise bean "value objects"? How do you persist value object instances?

  11. How can I prevent concurrent calls to the same stateful session bean?

  12. What are the issues involved in creating a singleton with enterprise beans?

  13. What happens when an enterprise bean throws an exception? How are exceptions propagated?

  14. What are some guidelines for using exceptions in J2EE?

  15. How should system exceptions be handled from an enterprise bean?



1.  How can I find a particular enterprise bean (component based on EJB technology)?

Enterprise bean instances reside in containers for EJB technology-based components (EJB containers), within servers enabled with EJB technology (EJB servers). A client accesses an enterprise bean by calling methods on the bean's remote interface. A client "finds" a particular enterprise bean instance by getting a remote reference (that is, an object that implements the remote interface) to that bean.

A client finds an enterprise bean by acquiring a reference to the bean's home interface, and then using "create" or "find" methods to retrieve a remote reference to the bean.

The sample code below shows how Web tier objects in the Java Pet Store find an enterprise bean's home interface, and then use it to find a specific bean instance on the server by getting a remote reference to the instance. This example uses the home interface "finder" method findByPrimaryKey()) to find a particular entity bean. The following code is adapted from the Java Pet Store class EJBUtil:

// ... class definition ...
public static InventoryHome getInventoryHome() {
   try {
      // Create a naming service object
      InitialContext initial = new InitialContext();

      // Obtain an abstract reference to the EJB's home interface.
      Object objref = initial.lookup("java:comp/env/ejb/inventory");

      // Convert the abstract reference to the home interface.
      InventoryHome ih =
         (InventoryHome) PortableRemoteObject.narrow(objref,
                                                     InventoryHome.class);
      return ih;
   } catch (NamingException ne) {
    ...
   }
}
This code returns a reference to the home interface, InventoryHome for entity bean InventoryEJB. It demonstrates the three steps to finding the home interface for an enterprise bean:

Once you have the home interface, you can then use a finder method like findByPrimaryKey() to get the enterprise bean instance you want. The finder method findByPrimaryKey() can then be used to access a specific entity bean, as occurs in the following excerpt from OrderHandler:


InventoryHome inventHome = EJBUtil.getInventoryHome();
    ...
    for (Iterator it = lineItems.iterator(); it.hasNext();) {
        LineItem LI = (LineItem)it.next();
        Inventory inventRef = inventHome.findByPrimaryKey(LI.getItemNo());
        inventRef.reduceQuantity(LI.getQty());
    }

Method findByPrimaryKey() takes a LineItem's item number as its argument, and returns the Inventory remote reference as a result.


2.  Should an enterprise bean look up the Java Database ConnectivityTM (JDBCTM) technology-based drivers directly from the ejbCreate() method?

No, enterprise beans should always access data sources using the JNDI interface to get a DataSource object. This allows the application server to optimize connection management. See the discussion on connection pooling below for more on this topic.


3.  What are some common pitfalls and practices to avoid when using enterprise beans?


4.  How can I take advantage of my application server's connection pooling features?

Most of the techniques for making most effective use of your EJB server's connection pooling feature involve properly managing the lifecycle of your enterprise beans and the connections they use. Here are some guidelines and explanations:

Following these guidelines will help your application to make most effective use of the application server's connection pooling mechanism.


5.  How can I hold and pass references to enterprise beans?

The EJB 1.1 specification does not prescribe a way for entity beans to store references to other entity beans. Application developers may choose from among several ways that entity beans can reference one another. These strategies include:


6.  How can nested transactions be implemented with enterprise beans?

The EJB 1.1 specification (§ 11.1.2) states that nested transactions are not supported. Requiring nested transactions would have shut out the many database vendors who do not support them. The specification leaves open the possibility adding nested transactions in the future.


7.  Can I use synchronization primitives in my enterprise beans?

Synchronization primitives include the synchronized keyword, as well as methods wait(), notify() and notifyAll() of class Object. All of these are disallowed in enterprise beans, as are other methods (such as Thread.interrupt(), etc.) that would affect the server thread pool.

Since the EJB server is completely responsible for managing threads and synchronization, there is absolutely no reason to use synchronization primitives in your enterprise beans.

While Vector and other classes that contain synchronized blocks is allowed in general, we recommend using the standard Java Collection classes (which are not synchronized) instead.

Restricting synchronization and thread control guards against the possiblity of deadlock. Enterprise bean instance access is synchronized automatically by the server at specific points in an enterprise bean's lifecycle. Allowing arbitrary synchronization control in enterprise beans could cause deadlock.

For more on restrictions placed on enterprise beans, see EJB Restrictions.


8.  How do I map a single entity bean to multiple tables?

If you use Bean-Managed Persistence (BMP), map the bean to the the tables manually. Consider applying the Data Access Object (DAO) design pattern to accomplish this.

If you're using Container-Managed Persistence (CMP), use the vendors object/relational mapping tool to specify the mapping between your object state and the persistence schema. Be aware that the CMP solution is not portable across application server vendors under the EJB 1.1 specification. If this is a concern, consider using BMP, and migrating to CMP under the EJB 2.0 specification or later.


9.  What does "stateless" mean in a stateless session bean? Can stateless session beans cache any information?

Stateless for a stateless session bean means that the bean can maintain no client-specific state. Stateless session beans can (and often do) have other state which is client-independent.

Client-independent data can be cached in a stateless bean if:

In most cases, any client-independent state that a stateless session bean may have will probably be indentical in all instances.


10.  What are enterprise bean "value objects"? How do you persist value object instances?

Value objects are objects that only have meaning within the context of another business object. They typically represent fairly fine-grained business concepts, like an address, credit card details and so on.

To persist an instance of a value class (which is a regular Java class, not an enterprise bean), use the class as a single property of an enterprise bean. For example, an Address class may be a value class used by class Person (so, in the example, class Person will have a method Address getAddress()). The Address may then be persisted as a whole using CMP. If BMP is used, you'll have to create persistence management code for class Address that manages moving Address data to and from the appropriate table(s).

For more on value objects, see the Value Object design pattern.


11.  How can I prevent concurrent calls to the same stateful session bean?

The EJB 1.1 specification (§ 6.5.6) specifically disallows multiple simultaneous accesses to the same stateful session bean. The EJB container throws a RemoteException if a client accesses a stateful session bean that is already serving a request from another client. (This is in contrast to entity and stateless session beans, which have new instances created by the container when concurrent calls occur.) Note that the concurrent call restriction implies disallowance of loopback calls on stateful session beans, as well.

Since stateful session beans correspond one-to-one with a user, multiple concurrent calls to a single stateful session bean instance is nonsensical. Nevertheless, there are several common cases where it may happen, for example:

Access to a stateful session bean can be serialized by creating a Web-tier class that acts as a proxy to a stateful session bean in the EJB tier. The proxy class would present have the same interface as the session enterprise bean, and would forward all calls to the session enterprise bean, but its methods would be synchronized. Keep in mind that other clients must be prevented from accessing the session enterprise bean remote interface directly.

The Java Pet Store sample application uses this technique in class ShoppingClientControllerWebImpl. Notice that the synchronized methods all defer their calls to another enterprise bean. This effectively serializes all concurrent calls from the Web tier to the bean in the EJB tier.


12.  What are the issues involved in creating a singleton with enterprise beans?

In general, the meaning of "singleton" in a distributed context is not entirely clear, particularly in the context of multiple Java virtual machines, clustered servers, and/or failover.

A network-wide singleton can be created as an Java RMI technology-enabled object and accessed through JNDI. Such a service, however, could become a single point of failure in the system: lose that service, and your entire distributed application could fail. Also, the J2EE 1.1 specification doesn't require that the server support an Java RMI registry, so the application will necessarily be portable across application servers.

Another option is to use an entity bean with a single primary key and store all state in a (potentially distributed) database. Note that containers may create multiple instances to serve concurrent requests for the primary key, so all state must be kept in a database. This solution could therefore easily become a performance bottleneck, as multiple clients wait for access to the same singleton object. This, however, is a concern with the general notion of "singleton" in a distributed environment (access to a singleton is serial, atomic access to a single object instance), not a limitation of enterprise bean technology.


13.  What happens when an enterprise bean throws an exception? How are exceptions propagated?

The EJB 1.1 specification (Chapter 12) defines two types of exceptions:

Application exceptions thrown by the enterprise bean layer are propagated to the client by the EJB container. All other enterprise bean exceptions, either RuntimeExceptions or subclasses of java.lang.Exception, are caught and handled by the container, which then throws a RemoteException to the calling client.

Enterprise beans are responsible for catching system exceptions and converting them to EJBExceptions.

See Chapter 12 of the EJB 1.1 specification for more on this topic. Pay particular attention to tables 8 and 9, which each present a matrix describing the method conditions and specified responses for various exception conditions and transaction demarcations.


14.  What are some guidelines for using exceptions in J2EE?

Application exceptions should be exclusively subclasses of Exception. Exceptions should report only exceptional application conditions from which the client can recover, and should not report system-level problems. Application exceptions do not as a rule cause client transaction rollback or server-side bean destruction. If rollback is desired when an exception occurs, catch the exception, and call EJBContext.setRollbackOnly() to mark the transaction (irrevocably) for rollback. The container will perform the rollback on behalf of the bean. Otherwise, clients can catch application exceptions and continue to communicate with the enterprise bean, with the transaction context intact. This method only works for beans with container-managed transactions.

The EJB 1.1 specification, § 9.2.6, contains a compatibility note pointing out that the EJB 1.0 technology practice of allowing business methods to throw non-application-level java.rmi.RemoteExceptions is now deprecated. Throw and EJBException or a RuntimeException to notify the container of non-application error conditions.


15.  How should system exceptions be handled from an enterprise bean?

All system exceptions must be caught by the bean and propagated to the container as EJBExceptions. RuntimeExceptions are the exception to this rule, and can be allowed to propagate to the Container without first being caught.


See also:

Session state in the EJB tier
EJB Restrictions

CONTENTS | PREV | NEXT | INDEX
Copyright © 2001 Sun Microsystems, Inc. All Rights Reserved.