|
Articles Index | J2EE Index | J2EE Developer's Corner Connection pooling is a technique used for sharing server resources among requesting clients. This article focuses on support for connection pooling for both database resources and nondatabase resources in a Java 2 Enterprise Edition (J2EE) environment. It examines the JDBC 2.0, JMS 1.02, JNDI 1.2 Standard Extension APIs with regard to connection pooling and looks at some existing vendor implementations of those APIs. This article then looks at the upcoming J2EE Connector Architecture 1.0 that would support a vendor-independent/pluggable approach to managing resource connections. The J2EE specification provides a distributed services-based architecture for implementing highly scalable, reliable, and available e-business applications. In general, a J2EE application architecture maps to the Model-View-Controller (MVC) framework -- repositories/external system resources support the domain model (Model), JSPs/Servlets manage the presentation (View), and EJBs deal with the business logic (Controller). A typical e-business application use case would be realized by components in all the three layers on the server side. Given the large number of user interactions (millions for customer-facing applications), the finite server-side resources need to be optimally shared. Such resources may include databases, message queues, directories, enterprise systems (SAP, CICS), and so forth, each of which is accessed by an application using a connection object that represents the resource entry point. Managing access to those shared resources is essential for meeting the high-performance requirements for J2EE applications Connection pooling is a technique that was pioneered by database vendors to allow multiple clients to share a cached set of connection objects that provide access to a database resource. In this article, I examine connection pooling in a J2EE environment for server-side resources such as databases, message queues, directories, and enterprise systems. Why pool resource connections?Consider the following code example where an EJB accesses a database resource using JDBC 1.0, without connection pooling:
Evidently, the main problem in this example is the opening and closing of connections. Given that entity beans are shared components, for every client request, the database connections are acquired and released several times. You can see from Figure 1 that acquiring and releasing database connections via the database manager, using JDBC 1.0, will impact the performance on the EJB layer. That impact is due to the overhead in creating and destroying those objects by the database resource manager process. Typically, the application server process takes around one to three seconds to establish a database connection (that includes communicating with the server, authenticating, and so forth), and that needs to be done for every client (EJB) request.
Connection pooling using service provider facilitiesNow I will look at what connection pooling facilities are currently available for database and nondatabase resource types in the J2EE environment. JDBC 2.0 Standard Extension APIThe JDBC 2.0 Standard Extension API specifies that a database service provider can implement a pooling technique that can allow multiple connection objects from a resource pool to be shared transparently among the requesting clients. In that situation, a J2EE component can use connection objects without causing overheads on the database resource manager, since a pool manager creates the connection objects upfront, at startup. The application service provider implements the pool manager in its memory space and can optimize resource usage by dynamically altering the pool size, based on demand. That is illustrated in Figure 2.
Using the
An XA (X/Open specification) equivalent exists for each of those interfaces as well as for XA connections. The following code example shows how an EJB application might access a database resource by using pooled connection objects (based on JDBC 2.0). The EJB component in this example uses a JNDI lookup to locate the database connection pool resource. The JNDI 1.2 Standard Extension API lets Java applications access objects in disparate directories and naming systems in a common way. Using the JNDI API, an application can look up a directory to locate any type of resource such as database servers, LDAP servers, print servers, message servers, file servers, and so forth. For a good overview of JNDI, refer to "The Java Naming and Directory Interface (JNDI): A More Open and Flexible Model." Note: The actual code will vary depending on the database vendor implementation classes.
The key difference between the above code (using JDBC 2.0) and using JDBC 1.0 is that a I should note that today almost all application servers employ a two-tier connection pooling architecture where the pools are held in the application server memory space (as opposed to a stand-alone connection broker). JMS 1.02 Standard Extension APIJ2EE application components can communicate asynchronously with other enterprise applications using a messaging resource. The JMS 1.02 Standard Extension API provides a vendor-independent way to communicate with messaging service providers. As in the case of a database resource, message queues are accessed using connection objects that can be pooled. The JMS 1.02 API includes the following interfaces to support resource pooling:
A JMS service provider implements those interfaces. The following code shows how an EJB component might access a message queue resource, using connection objects.
With connection pooling, the JMS factory classes typically have proxies (configured by an administrator) so the JNDI API for LDAP
The
Refer to the "Netscape Directory Server Application Programmer's Guide" for more details. The J2EE Connector Architecture 1.0In all of the above examples, the EJB components have to import vendor-specific implementation classes in order to use the connection pooling facilities of the resource. That obviously makes the EJBs less portable, weakening the J2EE promise. Ideally, one would like a generic connection interface that EJBs can use for any resource type and all connection management functions, including pooling, provided under the hood. That is one of the goals of the upcoming J2EE Connector Architecture 1.0 specification; a draft copy is publicly available at the time of this writing (see Resources).
Figure 3 shows the central concept behind the architecture, the resource adapter. A pluggable component for each resource type supported by the application server, a resource adapter executes within the application server address space. The client API for accessing those adapters could either be the Common Client Interface (CCI) or (for backward compatibility) a resource-specific API such as JDBC 2.0. For instance, the CCI defines
Connection pooling in Connector 1.0The programming model for Connector 1.0 is as follows:
For example, the iPlanet Unified Integration Framework Toolkit v 6.0, a product version of Sun's connectors to enterprise/legacy systems based on the upcoming EJB 2.0 connector architecture, defines connection pools for each backend system that an EJB layer might access. A thread, executed periodically, monitors the use and longevity of pool objects. For details, refer to iPlanet Unified Integration Framework. Design considerations for the EJB layerThe fact that you have resource managers that manage your connection pools does not guarantee optimum performance from the EJB layer -- there are some design considerations as well! First, consider the code example below of an EJB client accessing a LDAP directory that implements a connection pool.
What is wrong with the above picture? First, the stateful session object (
One alternative is that resource connections are acquired and released in the A second consideration is around the issue of authentication. You might have observed that pooled connections imply shared connections, which then imply that the connections are not tied to a specific authentication credential. For example, in the case of JDBC 2.0 connections, an application server pool manager requests, at startup, a preset number of connections from the DB manager, using a single authentication credential (typically a userid/password) stored in a configuration file. Sometimes that may not satisfy the security policy for the application. The same argument applies for LDAP connections that require binding with a specific credential to a LDAP subtree. In those situations, one alternative might be to use a cached connection that has been established using a specific credential, which can be reused for the same type of credentials. The downside to that is that cached connections are held for long periods of time. Another alternative might be to use generic connections for resources and implement certain application-level security. ConclusionIn this article, I showed the need for connection pooling resources in a J2EE environment, based on the shared nature of the resources as well as the EJB components that access them. You saw the facilities defined by the JDBC 2.0, JMS 1.02, and the JNDI 1.2 Standard Extension APIs as well as vendor support for the implementation of those API interfaces. Although the vendor-specific solutions are robust, you use them at the cost of EJB portability. The upcoming J2EE Connector Architecture 1.0 addresses that issue and makes resources pluggable, relieving the EJB layer from dealing with vendor-specific libraries. And finally, I explained why your design plays an important role in taking advantage of those pooling techniques for delivering high-performance J2EE applications. Resources
About the AuthorSiva Visveswaran has been working with Java for more than three years and has been smitten by J2EE. Most recently, he has been involved in architecture and development of large and complex J2EE applications for Fortune 100 financial service companies and dot-coms. He has more than 12 years experience in the IT industry and is currently working as a principal consultant at a large management consulting firm, specializing in e-business architecture, infrastructure technologies, and content management solutions. Siva has a master's degree in computer science from Wayne State University in Detroit, Mich.
Reprinted with permission from the October 2000 edition of JavaWorld magazine. Copyright ITworld.com, Inc., an IDG Communications company. Register for editorial e-mail alerts. | ||||||||||||||
|
| ||||||||||||