Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

3.9 Overview of the EJB API

You may prefer to have a look at the example of a complete EJB in the next chapter before reading this section. Although this information is important, you will be able to follow what is involved in constructing and testing an EJB without this.

To ensure a uniform API between the EJB, the container, and the clients, the various parts of an EJB are required to implement certain interfaces. The most important of these are described briefly here. All these interfaces and their methods will be explained in more detail later in the book.

3.9.1 EJBHome and EJBLocalHome

All remote home interfaces must extend javax.ejb.EJBHome, while local home interfaces extend javax.ejb.EJBLocalHome. No special effort is required on the part of the developer to implement the methods specified in this interface; the developer’s home interface is implemented by the container’s proxies, which are generated by the server vendor’s tools, usually at deployment time. The methods in EJBHome are summarized in Table 3.2. EJB clients can call any of these methods, as well the ‘create’ and ‘find’ methods defined by the developer, and they are handled by the home stub in conjunction with the home object and the container. EJBLocalHome only defines the method remove(), and is therefore much simpler. This reflects the comparative simplicity of local EJB interaction.

Table 3.2. The EJBHome interface.

EJBMetaData getEJBMetaData()

Returns an EJBMetaData object that provides information about the structure of the EJB, including the names of the Java classes that form its home and remote interfaces, its primary key (if applicable), and the type of the EJB. This method is not commonly used in application programs, but may be useful for development tools.

HomeHandle getHomeHandle()

Returns a HomeHandle object that is a serializable version of the home stub. The client can pass the handle to another client or use it to store a reference to the home object in a file. This method is rarely used, because any client can get a reference to the home object by doing a JNDI lookup.

void remove (Object pk)

Removes the EJB with the specific primary key. This method is only meaningful for entity EJBs, as session EJBs don’t have primary keys.

void remove (Handle h)

Removes the EJB with the specific handle. Don’t confuse an EJB handle with a home handle; an EJB handle identifies a specific EJB. This method can be used on both session and entity EJBs. In practice, it is more convenient to remove an EJB through its remote interface, as described below.


Note that the EJBHome interface specifies ‘remove’ methods but no ‘create’ methods. This is because the create() methods have arbitrary parameter lists, and must therefore be specified in the developer’s home interface, rather than in a general, API-level interface. remove() always takes no arguments.

3.9.2 EJBObject and EJBLocalObject

All remote interfaces—required to support the distributed client view—implement the EJBObject interface, and therefore the client can call these methods on the EJB reference obtained from the home object, as well as any methods defined in the remote interface itself. As was the case for the EJBHome interface, the methods specified by EJBObject are not implemented by any part of the EJB; they are implemented by the remote stub, which is generated automatically. Table 3.3 summarizes the methods specified by EJBObject. EJBLocalObject supports a subset of these methods, appropriate to intra-JVM operation.

Table 3.3. The EJBObject interface.

EJBHome getEJBHome()

Returns a reference to the home object for the EJB. Technically, what we get is a reference to a home stub, on which we can make remote calls on the real home object. This method is seldom used, as we can always get a reference to the home by doing a JNDI lookup.

Handle getHandle()

Returns a Handle object that identifies the EJB object and is serializable. Note that if we try to serialize the EJB reference itself (that is, the result of calling ‘create’ or ‘find’ on the home interface), this may fail; this is because the instance is really a stub, not a real object. Stubs may encapsulate active network endpoints, and are therefore not guaranteed to be serializable (see page 486). If we want to serialize an EJB reference, we can get the Handle and serialize that. After deserialization, we call getEJBObject() on the handle to get a real EJB reference. A particular use for EJB handles is to store a reference to an EJB within an HttpSession object in a servlet. This allows successive client interactions with the servlet to use the same EJB.

Object getPrimaryKey()

Returns the primary key for the EJB, if applicable. This method is only meaningful for entity beans, as only entity beans have primary keys. This method is of limited use in EJB applications, as the EJB developer will probably provide a mechanism for clients to get the data represented by the primary key.

boolean isIdentical (EJBObject o)

Determines whether the EJB is identical to another one. The notion of ‘identicality’ is well-defined for entity beans: Two EJBs are identical only if they have the same primary key. Identicality is less well-defined for session beans: The EJB Specification says that for stateful session beans, they are identical if two references are both associated with the same implementation class instance (unlikely in practice). For stateless session beans, this method always returns true, as by definition stateless session beans are indistinguishable. These issues are described in more detail in Chapter 6.

void remove()

Remove the EJB.


3.9.3 SessionBean

This interface is implemented by all session EJB implementation classes. As these methods will be discussed in much more detail in Chapter 6, only a brief description is given here (Table 3.4).

Table 3.4. The SessionBean interface.

void ejbActivate()

This method is called by the EJB container on the implementation when it has reactivated the EJB from its ‘passive’ state. See page 149 for more details. This method is never called on stateless session EJB, and is often empty even in stateful session EJBs.

void ejbPassivate()

This method is called by the EJB container on the implementation when it is about to be set into its ‘passive’ state. See page 149 for more details. This method is never called on stateless session EJB, and is often empty even in stateful session EJBs.

void ejbRemove()

This method is called by the EJB container when the implementation is no longer required. The container will then release its reference to the instance, and the garbage collector will eventually delete it and reclaim its memory.

void setSessionContext (SessionContext sc)

This method is called by the container immediately after creating the instance of the implementation. The method is passed a SessionContext instance which the EJB should store. Later it can use this object to obtain information about transactions and security.


The SessionBean interface extends EnterpriseBean, which has no methods but extends java.io.Serializable. Thus all session EJB implementation classes are serializable by default.

3.9.4 EntityBean

This interface is implemented by all entity EJB implementation classes. As these methods will be discussed in much more detail in Chapter 11, only a brief description is given here (Table 3.5).

The EntityBean interface extends EnterpriseBean, which has no methods but extends java.io.Serializable. This all entity EJB implementation classes are serializable by default.

Table 3.5. The EntityBean interface.

void ejbActivate()

This method is called by the EJB container on the implementation when it has reactivated the EJB from its ‘passive’ state. See page 330 for more details.

void ejbPassivate()

This method is called by the EJB container on the implementation when it is about to be set into its ‘passive’ state. See page 330 for more details.

void ejbRemove()

This method is called by the EJB container when the client calls remove() on the EJB reference. The EJB should remove the data that corresponds to this EJB. The container does not remove the instance (as was the case for session EJBs), as it can be reused with a different set of data.

void setEntityContext (EntityContext sc)

This method is called by the container immediately after creating the instance of the implementation. The method is passed an EntityContext instance which the EJB should store. Later it can use this object to obtain information about transactions and security.

void unsetEntityContext()

This method is called by the container immediately before being made eligible for garbage collection. That is, it is the last method that the container will call on the implementation.


3.9.5 EJBContext, SessionContext, and EntityContext

Objects that implement SessionContext and EntityContext are passed to an EJB’s first initialization method as soon as the container creates it. This method is setEntityContext() for entity EJBs and setSessionContext for session EJBs. Both these interfaces are derived from EJBContext, and it is this superinterface that specifies most of the methods. The purpose of these interfaces is to allow EJBs to find out various things about their operating context, particularly the caller’s security attributes and (in entity beans) the primary key.

The EJBContext interface is explained briefly in Table 3.6, SessionContent in Table 3.7, and EntityContext in Table 3.8.

You may have noticed that the method getEJBObject() appears (identically) in both EntityContext and SessionContext, even though these interfaces have a common superinterface.

Table 3.6. The EJBContext interface. Note that deprecated methods are not shown.

Principal getCallerPrincipal()

Returns an object that implements the javax.security.Principal interface, which provides information about the entity (person or thing) that is calling this method. If the EJB is being called outside of an authenticated client container (e.g., the application does not provide a log-in procedure), then this method returns the Principal ‘guest.’ Principal has one useful method: getName(). There are some issues associated with this method that the developer needs to be careful about. See Chapter 16 for full details.

EJBHome getEJBHome()

Gets the home object for this EJB or, rather, a client stub that implements the home interface and can communicate with the home object. Thus, if this result is passed to another EJB, that EJB can call methods on the home object via the container, rather than making direct Java method calls within the JVM. Why is this important? In a distributed environment, the EJB that the reference is being passed to may not be on the same server, and therefore not in the same JVM. Thus, the result from getEJBHome is a safe way for an EJB to pass its home interface to other EJBs. The EJB can also use it to find or create other EJBs of the same type.

EJBLocalHome getEJBLocalHome()

Gets the local home object for this EJB, if it provides a local client view. The object returned is a local reference to an object in the same JVM, not a stub.

boolean isCallerInRole(String role)

In brief, this method determines whether the caller of the method is able to play a specific security role. EJB security is largely based on the idea of roles, more than user IDs or groups. Determining and setting up security roles is a key part of EJB application design. This is a very important topic, and it has a whole chapter (Chapter 16) devoted to it.

boolean getRollbackOnly()

An EJB can call this method to determine if it is currently involved in a transaction that has already been set to roll back. This issue is covered in detail in Chapter 9.

void setRollbackOnly()

If this method is called, any transaction that is currently in progress will be rolled back rather than committing. See Chapter 9.

UserTransaction getUserTransaction()

Returns an object that implements the javax. transactions.UserTransaction interface. Using this object, an EJB can begin, commit, and roll back transactions. See Chapter 9.


Table 3.7. The SessionContext interface. This is a subinterface of EJBContext, so the session EJB has access to the methods in EJBContext as well.

EJBObject getEJBObject()

The object returned by this method—which will implement the javax.ejb.EJBObject interface—is the equivalent to the this reference in nondistributed programming. That is, if an EJB wants to pass a reference to itself to another EJB, or to a client that is not in the same JVM, it should pass the result of getEJBObject(), and not this. What the caller will actually get is a stub that can be used to make remote method calls on the EJB. This is extremely important. Remember that under IIOP objects are passed by value, not by reference. If an EJB passes this to another EJB, and that EJB is not in the same JVM, then the recipient will get a copy of the EJB implementation, not a reference to it. Making method calls on this copy will only affect the copy, not the real EJB.

EJBObject getEJBLocalObject()

If the EJB provides a local client view, this method returns a reference to the local object. This object can be used in the same was as the this pointer.

Table 3.8. The EntityContext interface. This is a subinterface of EJBContext, so the entity EJB has access to the methods in EJBContext as well.

EJBObject getEJBObject()

Has exactly the effect as SessionContext. getEJBObject(). See above for discussion.

EJBLocalObject getEJBLocalObject()

Has exactly the effect as SessionContext. getEJBLocalObject(). See above for discussion.

Object getPrimaryKey()

Returns the primary key for this entity. The primary key uniquely identifies entity EJBs of the same class.


  • + Share This
  • 🔖 Save To Your Account