Home > Articles > Programming > Java

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.


InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020