Home > Articles > Programming > Java

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

3.8 The EJB container and its proxies

In Java RMI, 10 the clients of a remote object did not call methods on the remote object directly. Instead, they call methods on a stub, which passes the method call information over the network to the ‘skeleton,’ which calls the method on the remote object. The EJB model extends this notion by introducing the idea of the EJB container as an additional element in the interaction. The container has two main functions:

  • It intercepts all method calls made by the client and carries out various actions (concerned with security and transaction management, among other things) before delegating them to an instance of the implementation class itself. That is, the container provides proxies that isolate the EJB’s implementation class from its clients.

  • It provides general services to the EJB, such as database connection pooling.

3.8.1 Container proxies

As we have seen, a remote client of a session or entity EJB makes calls on stubs that implement the home interface and remote interface. These stubs communicate with the home object and remote object, which can be considered part of the EJB container. These proxies then delegate the method calls to the EJB where necessary. Because the EJB server does not know in advance what methods will be provided in your EJBs, it can’t provide general-purpose proxies. Such proxies must be generated for each new EJB that is deployed. The server vendor will provide tools to do this, usually at deployment time. The relationship between the proxies and the stubs is show diagrammatically in Figure 3.4.

Where do the home interface and remote interface fit into all of this? The only Java class that must implement the home interface is the stub that the client uses to communicate with the home object. Similarly, the stub for the EJB object must implement the remote interface. These stubs must implement the interfaces, because the client will call methods on the stubs via the interfaces. At compilation time, the client will likely not have access to the stubs themselves, but at runtime we can be sure that they will implement methods that the client will call.

However, there is no need for the (remote) home object or (remote) EJB object to implement any of the EJB developer’s interfaces. These proxy objects have methods that are called by the skeletons—in a vendor-specific manner, not directly by the client. They therefore don’t need to implement any of the EJB’s interfaces. They may implement them, if the server vendor believes that this is helpful to the implementation of the server, but it is irrelevant to the EJB developer.

If it is irrelevant, why have I mention it at all? The reason is that many introductory textbooks and courses describe the home object as an implementation of the home interface and the EJB object as an implementation of the remote interface. This supports the view that the client ‘calls methods on the home object’ or on the EJB object. This oversimplification obscures the roles of the stubs and skeletons, and the fact that client-EJB interactions using the remote client view are always network operations, with the overheads and complexities that this entails.

We will now consider the functions of the home object and EJB object in more detail. Unless otherwise stated, the comments in this section apply equally to the proxies supporting the local and distributed client views.

The home object

As we have already seen, the home object for each EJB is instantiated as soon as the EJB is deployed, or whenever the server is restarted. The home object must be available at all times, as the client will use it to get access to the EJB.

The home object is responsible for responding to ‘create’ and ‘find’ calls from the client. When the client calls a ‘create’ method, the home object brings about the creation of an instance of the implementation, if necessary (or returns one from the pool). It also causes an EJB object to be instantiated, if necessary. When the client makes a ‘find’ call, the home object either locates the EJB itself or delegates the find call to the implementation. These issues will be discussed in more detail in later chapters.

With distributed interaction, the class on the client that encapsulates the communication between the client and the home object is called the home stub.

The EJB object

In this section, the term ‘EJB object’ refers to both remote and local EJB objects.

The EJB object acts as a proxy for the EJB’s business methods. When the client makes a call on a business method, it is actually making the call on to the EJB object, which carries out two important actions before calling the method itself. First, it checks whether the client is authorized to call the requested method. If not, it throws an exception. If the call is allowed, the EJB object checks whether a new transaction needs to be initiated (based on the transaction attributes supplied by the developer).

After calling the method on the EJB, the EJB object attempts to commit any transaction it started before the call.

It is important to understand that there is not necessarily a one-to-one relationship between EJB object instances and implementation class instances, nor is there a one-to-one relationship between clients and EJB objects. Failure to understand this principle is a major cause of errors in EJB development. The relationship between the instantiation of EJB objects and implementation class instances will be discussed in more detail in the chapters on session EJBs and entity EJBs.

An important feature of the EJB object is to provide the EJB with the illusion that it is running in a single-threaded environment. This relieves the developer of the need to include thread management and synchronization code. The techniques it uses to do this are discussed in Chapter 6.

With a remote client, the class deployed on the client that encapsulates the communication between the client and the EJB object is called the remote stub. Local clients don’t use stubs, as the interactions are in the same JVM.

3.8.2 ‘EJB skeletons’

In Java RMI, the client and server endpoints of the remote method protocol were supported by a stub and a skeleton. It was the skeleton that made the method calls on the remote object. It did this directly at the behest of clients, without interacting with any supporting infrastructure. For example, the skeleton would never take it upon itself to determine whether the caller was allowed to make the method call. In a sense, the stub and skeleton were nothing more than a mechanism to conceal the networking operations.

With EJBs, the situation is more complicated. Between the network and EJB implementation class, two types of supporting functionality are required. First, we have to encapsulate the network operations, as for Java RMI. Second, we need to fulfil the contract between the proxy objects and the implementation class. This contract says, for example, that the proxies can initiate database transactions, carry out security checks, and so on.

All this means that the proxy objects—the home object and EJB object—are not merely skeletons in the Java RMI sense. However, there is no logical reason why skeleton (networking) functionality cannot be embedded into these objects along with their other responsibilities. That is, the stubs on the client could communicate directly with the proxies, which would fulfil this ‘dual role.’

Of course, this is only possible if there is good collaboration between the authors of the software that generates the stubs and those of the software that generates the proxies. This is likely to be the case only when both of these entities are generated by software from the same vendor.

To allow for increased flexibility, and to allow different RMI protocols to be supported with the same proxies, most vendors have chosen to implement separate skeletons for the EJB proxies, rather than allowing the proxies to be skeletons as well. In a scheme of this sort, when the client calls an EJB method, it calls the method on the stub, which communicates with the skeleton, which then calls a method on the proxy, which calls the EJB method. This slight increase in overhead is compensated by an increase in flexibility.

3.8.3 Container services

As well as its role in isolating EJBs from their clients, the container provides various services to the EJB. One of the most important of these is database connection pooling. When an EJB wants to make a connection to a database, it gets this connection from the container, not from a JDBC driver manager. This allows the container to pool database connections, and share them between different clients. With this strategy, an EJB application can support many clients while using only a few database connections. Database connection pooling is described in more detail in Chapter 8.

An EJB relies on its container for access to all external resources, not just database connections. This principle allows the container to manage resources efficiently while maintaining the scope of transactions and propagating security contexts, as will be discussed later. The problem with this strategy is that it can lead to problems, when EJBs need access to resources of a type that the container did not anticipate. This is by no means an uncommon situation: EJB applications frequently need access to legacy systems, networked services, or hardware devices.

Most EJB server products provide an extension API, which developers can use to extend the functionality of the container. The problem with this approach is that it is vendor-specific: Applications that rely on extension APIs will not be portable. The J2EE Connector Architecture—recently finalized—provides a vendor-independent mechanism for extending an EJB container to deal with nonstandard external resources. The connector API is described in detail in Chapter 18.

3.8.4 EJB instance pooling

The EJB architecture is designed, as far as possible, to allow the EJB container to pool instances of the implementation class and share them amongst clients. By doing this, we avoid the overhead of creating and garbage-collecting the large number of instances that would otherwise be required to support large client volumes.

Instance pooling is usually managed by EJB objects in collaboration with other parts of the EJB container (Figure 3.8). In the simple scheme shown, a single, shared EJB object is managing two implementation instances on behalf of a number of clients. It is important to remember that the EJB object must prevent an instance of the EJB’s implementation class from being entered by more than one thread at a time, so the object must have a notion of which instances are serving clients at any given time. If the EJB object is itself pooled or shared, and can accept multithreaded calls from skeletons, then it must itself must be thread-safe. When a client calls a method, the EJB object will make a call on one of the instances of the implementation class, if one is available. Otherwise, it will simply wait until an instance is free. This does impose a delay on the response to the client, so clearly there is an advantage to having enough instances that clients will not usually have to wait long. On the other hand, the server hardware cannot support an arbitrarily large number of active threads, so there may not be any advantage to having a very large pool. 10–30 instances seems to work well for most practical applications.

Figure 3.8Figure 3.8 . Instance pooling with a shared EJB object. See text (Section 3.8.4) for details.

The developer cannot assume that the EJB container will direct a client request to any particular instance in the pool. 11 The client may well find that if it calls the same method twice, the second call involves a different instance than the first. Clearly this will have implications for designers. These implications for different types of EJB will be described in detail in later chapters.

Not all EJB types can be pooled. In particular, it is difficult to pool stateful session EJBs, as we shall see.

  • + Share This
  • 🔖 Save To Your Account

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.


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.


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.


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.


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


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


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.


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.


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