Archive for July, 2009

Chapter 13: JDBC Connectivity The role of a

Friday, July 31st, 2009

Chapter 13: JDBC Connectivity The role of a Web-tier container as an intermediary between client Web applications and an RDBMS is defined by the set of J2EE specifications most recently in the Servlet 2.4 specifications and the JDBC 3 specifications (located at http://jcp.org/aboutJava/communityprocess/final/jsr154/and http://java.sun.com/products/jdbc/download.html#corespec30). JDBC 4 specification is also at the same link, but Tomcat 6 support is forthcoming. The value-added service that Tomcat provides is compliant with these specifications, and is documented as a three-tier architecture (see Figure 13-3 ). Jakarta Commons DBCP pool manager JNDI emulation 1. Lookup via JNDI Referenced JDBC data source 2. Receive pooled data source Tomcat Container JNDI Resource Definitions in server.xml Web application Figure 13-3: JNDI lookup of a JDBC data source in Tomcat 6 Figure 13-3 shows how JDBC drivers are configured with Tomcat as JNDI resources. These resources are made available during Web application runtime via standard JNDI lookups. The following steps are depicted in the diagram: 1. A Web application obtains a JNDI InitialContext from Tomcat; it then performs a lookup on the resource (JDBC data source) by name. 2. Tomcat handles the JNDI lookup by consulting the configuration files ( context.xml, server.xml, and web.xml) to determine which JDBC driver to use for obtaining a data source. Tomcat can also use database connection pooling (DBCP) to pool the connections made; the connections obtained from Tomcat are logical connections. Even though no true JNDI-compatible directory services are involved, the Tomcat container emulates the action of a JNDI provider. This enables code that is written with JNDI as the JDBC data source lookup mechanism to work within the Tomcat container (and other Servlet 2.4 compliant application servers). As you can see in Figure 13-3 , Tomcat 6 does more than merely provide JNDI emulation. It can also provide database connection pooling. Tomcat 6 uses Apache Commons DBCP (database connection pooling) for its built-in pool manager functionality.

If you looking for unlimited one inclusive web hosting plan please check web hosting plan website.

Chapter 13: JDBC Connectivity JDBC 4 corrected this

Thursday, July 30th, 2009

Chapter 13: JDBC Connectivity JDBC 4 corrected this problem by providing a way for the pool manager to ask a connection if it is still valid. For this to work properly, it will require: . The JDBC driver to implement this JDBC 4 feature, enabling a connection s validity to be queried . The application server pool manager to support this feature to query connection validity regularly to avoid having bad connections remaining in the pool At the time of this writing, JDBC 4 specification has just been finalized, and implementations from commercial vendors or open source products are just starting to be available for early access. Tomcat and the JDBC Evolution Application developers and system designers using Tomcat 6 have a wide choice of JDBC support mechanisms from which to choose. Tomcat 6 provides JDBC 3 support while offering full backward compatibility to JDBC 2 (as well as JDBC 1). The remainder of this chapter examines the recommended mechanism to access JDBC resources while working with Tomcat, and explores one alternative access mechanism. The major new JDBC features that are part of Tomcat 6 include the following: . Application server managed database connection pools: Tomcat 6 uses Jakarta Commons Database Connection Pooling (DBCP) to provide container-managed connection pooling. This also enables flexible configuration for the pooling mechanism (see the section Resource and ResourceParams, later in this chapter). JDBC 3 is the first specification that defines standard configuration parameters for pooling (such as maxStatements, initialPoolSize, minPoolSize, maxPoolSize, and maxIdleTime), making the mechanism more configurable in a standards-compliant manner. . Using the JNDI-API to look up data sources within an application server: Tomcat 6 emulates JNDI for Web applications running under it. This is a portable and configurable way of obtaining data sources for JDBC operations without hard-coding the driver and associated properties. It makes the selection of the JDBC driver and RDBMS instance a deferred deployment-time decision. JDBC 3 specifies JNDI as the preferred method for applications to locate a data source. . Ease of migration to the connector architecture: Tomcat 6 s decoupled architecture for access to JDBC data sources (through JNDI lookup) is a first step in the migration toward the JCA Connector based architecture. JNDI Emulation and Pooling in Tomcat 6 Tomcat provides valuable services for hosted Web applications that use JDBC connections. More specifically, Tomcat enables running Web applications to do the following: . Access JDBC data sources using standard JNDI lookup . Use connection pooling value-added service

For reliable and cheap web hosting services please check cheap web hosting website.

Chapter 13: JDBC Connectivity Database connections pool manager

Thursday, July 30th, 2009

Chapter 13: JDBC Connectivity Database connections pool manager Web application Physical databaseconnections in pool 1. Request connection 3. Close logical connection 2. Receive logical connection RDBMS 4. Physical connection (returned to the pool) Figure 13-2: JDBC connection pooling A pool manager creates the initial physical connections, manages the distribution of the physical connections to the Web applications in the form of a logical connection, returns any closed logical connection to the pool, and handles any exception or error that may arise potentially disconnecting the physical connection or recovering from error conditions. Note that closing a logical connection does not actually close any physical connection, but merely returns the connection back to the pool. This pool manager functionality may be provided by one of the following three sources: . An application server such as Tomcat 6 . A third-party pool manager software vendor . The JDBC driver vendor When configuring Web applications to run on Tomcat 6, the preferred and recommended pool manager to use is one that is supplied with the Tomcat server. A Problem with Connection Pooling Since the introduction of JDBC 3 and application server connections pooling in general, Tomcat users have gained some experience in the use of connection pools. Over time, connection pools can sometimes develop problems. Since database connections are made over the network, they are not 100 percent reliable. Connections can sometimes fail in a way that you cannot recover from. Over time, since connections in a pool are kept open physically, many connections in the pool may start to go bad. The bad connections still hold up resources, and appear to be working to the pool manager. With JDBC 3 pooling, there is no reliable way of asking the connections if they are still valid. As a result, sometimes all connections in the pool need to be flushed, requiring a server restart.

For high quality java hosting services please check java web hosting website.

Chapter 13: JDBC Connectivity administrative and maintenance headaches.

Wednesday, July 29th, 2009

Chapter 13: JDBC Connectivity administrative and maintenance headaches. These drivers are also typically hardware/ OS-specific (because of the data access mechanism that they depend on), making them completely nonportable. . Type II: These drivers are partially written in Java and partially written in native data access languages (typically C or C++). The non-Java portion of these drivers limits the portability of the final code and platform-migration possibilities. The administrative and maintenance burden of Type I still exists. . Type III: These drivers are pure Java drivers on the client side, which gives them the portability benefit of Java. However, they rely on a middleware engine running externally to operate. The client code communicates with the middleware engine, and the engine talks to the different types of databases. The administration and maintenance burden is somewhat reduced, but far from eliminated. . Type IV: These drivers are 100 percent Java and talk directly to the network protocols supported by the RDBMSs. This results in the highest performance connection and the most portable application code. Administration and maintenance are greatly simplified (only the driver needs to be updated). Fortunately, most modern day JDBC drivers are of the Type IV variety. All the major RDBMSs available today (MySQL, Oracle, DB2, and MS SQL Server) have Type IV JDBC drivers available, either through the database vendors themselves or via a third-party driver vendor. Database Connection Pooling When a Web application accesses a remote RDBMS, it may do so through a JDBC connection. Typically, a physical JDBC connection is established between the client application and the RDBMS server via a TCP/IP connection. Establishing such a connection is CPU-, memory-, and execution time intensive. It involves multiple layers of software, and the transmission and receipt of network data. A typical physical database connection may take several seconds to establish. Contrast this with the cost of doing a simple database query, which typically takes milliseconds to complete. It is obvious why it would be wise to decrease the number of connects and disconnects between queries. Modern Web applications consist of JSPs and servlets that may need data from a database on every HTTP request (for example, a JSP that prints the current employees from a specific department, or an electronic auction system that enables you to see all your current open bids). On a well-loaded server, the time it takes to establish, disconnect, and reestablish actual connections (physical connections) can substantially slow down Web-application performance. To create high-performance and scalable Web applications, JDBC driver vendors and application servers are incorporating database connection pooling into their products. Connection pooling reduces expensive session establishment times (connects, disconnects, and reconnects) by creating a pool of physical connections when the system starts up. When an application requires a connection, one of these physical connections is provided. Typically, when an application finishes using a connection, it is disconnected. However, in the case of a logical connection, the associated physical connection is merely returned to the pool and awaits the next application request. Figure 13-2 illustrates database connection pooling.

For high quality website hosting services please check tomcat web hosting website.

Chapter 13: JDBC Connectivity solve, and code can

Tuesday, July 28th, 2009

Chapter 13: JDBC Connectivity solve, and code can quickly become vendor-specific again (this time, depending on the JDBC driver vendor). Furthermore, the rapid maturation of J2EE has consolidated its overall architecture. There is growing momentum to adopt the same resource adapter model to the Enterprise Information System (EIS) throughout the J2EE stack. Architecturally, JDBC connections are connections to external/legacy systems, which are considered part of the EIS. In the J2EE architecture, these connections should be managed through a well-defined Connector architecture. This is the subject matter of J2EE Connector Architecture (JCA). For more information, see the following URL: http://java.sun.com/j2ee/connector/ In this architecture, J2EE software components access EIS resources via resource adapters with a common set of well-defined interfaces. These interfaces enforce well-defined contracts (between application server and resource adapter) in connection management, transaction management, and security. The evolution of the JDBC standard is migrating to this new JCA architecture as it becomes better defined. The first step toward this migration is to detach any direct coupling between the application logic and the specific EIS resource that it needs. This can be accomplished by an intermediary indirect lookup mechanism. JNDI is a Java-based industry standard that can serve this purpose. JDBC 3 is the first version to be designed with this migration in mind. In fact, JDBC 3 is the first specification that clearly spells out the different architectures that JDBC can operate in including two-tier and three-tier models. The three-tier model corresponds to the application server model and is the model of operation favored by J2EE applications. The JDBC specification also attempts to accommodate JDBC 1 and 2 drivers and model of operations, while formalizing JNDI as the preferred way for applications to obtain a data source. It also formalizes connection pooling as a value-added service of the application server or Servlet container. JDBC 4, finalized in December of 2006, continues to support all features of the previous three versions. In addition, JDBC 4 addresses some features that make development easier, improve connection pooling, expose physical database row IDs for developers, and introduce a new XML data type to JDBC. With the introduction of Tomcat 6 in early 2007, JDBC 4 is still in its infancy in terms of vendor support. Regardless of the JDBC version, the JDBC driver still must translate the unified JDBC commands into native commands to connect to the different servers. JDBC drivers have evolved significantly over the past few years and most of them today are high-performance Type IV drivers (explained in the next section). However, some legacy systems still exist that support only the older Type I to Type III drivers. It is a good idea to gain some familiarity with different types of JDBC drivers that may be around. JDBC Driver Types There are four different types of JDBC drivers: Type I to Type IV. In general, the higher driver types represent an improvement on architecture and performance, as described here: . Type I: These drivers are the most primitive JDBC drivers, as they are essentially data access adapters. They adapt another data access mechanism (such as ODBC) to JDBC. These drivers completely rely on the other data access mechanism to work and, as such, create double the

For reliable and cheap web hosting services please check tomcat web hosting website.

Chapter 13: JDBC Connectivity In JDBC programming, developers

Monday, July 27th, 2009

Chapter 13: JDBC Connectivity In JDBC programming, developers typically perform the following steps: 1. Obtain a connection to the remote database server (in JDBC 1. x, it is necessary to instantiate a database driver prior to obtaining a connection). 2. Create and prepare an SQL statement for execution (or call a stored procedure in the RDBMS). 3. Execute the SQL statement. 4. Obtain the returned result set (if any) and work on it. 5. Disconnect from the remote database. Administrators are most interested in facilitating the first step obtaining a connection to the desired database. Establishing and Terminating Connections to RDBMS s Other than providing a unified way of accessing, modifying, and manipulating data in RDBMSs, JDBC also provides a unified way to connect to RDBMSs from different vendors. While normal native connections to Oracle will be very different from connections to MySQL (which will be different from working with Microsoft s SQL server), connecting to any of these RDBMSs can be accomplished using the same JDBC API calls. Evolving JDBC Versions In the early days of JDBC, most Java developers were coding to the JDBC 1 standard. Under this standard, all code that needed to establish a connection to an RDBMS (as well as the code to disconnect from the RDBMS) was written by the developers. In fact, even the code to select and activate a JDBC driver was coded by the developers. While simple and straightforward to code, this approach created a problem; in some cases where the driver used was hard-coded by the developers, the code to obtain a connection worked only with RDBMSs from a specific vendor. With the arrival of JDBC 2, this restriction was relaxed. JDBC 2 introduced the concept of a data source. This is an indirect way of specifying the JDBC driver to be used for making the connection. Developers can now obtain a connection from the data source in their code, enabling the same JDBC code to work with drivers from any vendor. Meanwhile, an administrator can switch database vendor support by simply configuring a different data source, and no code changes are needed. The selection and configuration of data sources shifted from the developer to the administrator. As Web applications became more complex, the demand for high-performance concurrent access to database connections increased. The code that developers write to maintain and share database connections becomes highly complex and error-prone. Because this code is utilitarian, and can be used by all applications, it is another area that JDBC 2 attempts to improve on (see the section Database Connection Pooling, later in this chapter). While JDBC 2 s introduction of data source and connection pooling support opens up new possibilities for RDBMS developers, it falls short of specifying standard ways in which these features should (or must) be used. As a result, many of the architectural issues are left for the JDBC driver writers to

For high quality java hosting services please check java web hosting website.

Chapter 13: JDBC Connectivity JDBC Basics JDBC is

Monday, July 27th, 2009

Chapter 13: JDBC Connectivity JDBC Basics JDBC is a programming interface for accessing RDBMSs. Its operation is based on the transmission and execution of Structured Query Language (SQL) on the database server. SQL is a text-based query language for performing operations with data stored in a relational database. In fact, JDBC is based on a Call-Level Interface (CLI) to an engine that processes SQL statements. More specifically, JDBC uses the X/Open SQL CLI (X/Open is an international standards organization) conforming to the SQL92 language syntax standard. Figure 13-1 illustrates how SQL CLI, and therefore JDBC, operates under the hood. Java applications JDBC library JDBC driver SQL CLI processor Data tables Translated or pass-through command line SQL Returned results or status code Java API calls JDBC API RDBMS Figure 13-1: JDBC operation model In Figure 13-1 , the JDBC engine submits SQL query statements to the remote SQL processing engine (part of the RDBMS, it typically handles multiple simultaneous connections via a connection manager), and the SQL processing engine returns the result of the query in a set of data called a result set . A result set is typically zero or more rows of data. Think of result sets as a temporarily created table. JDBC operations are designed to do the following: . Take the JDBC API calls and transform them into an SQL query . Submit that query to the SQL processing engine on the RDBMS . Retrieve the result set that is returned from the query and transform it into a Java-accessible data structure Not all statements return a result set. If a search is not successful, the returned result set will be empty (called a NULL result set). In addition, the SQL language includes statements that are used to create tables, update data, delete rows, and so on; these statements do not return any result sets either.

For high quality jboss hosting services please check jboss web hosting website.

JDBC Connectivity Most Web applications process data, and

Sunday, July 26th, 2009

JDBC Connectivity Most Web applications process data, and that data is often stored in a database. The most popular database management systems are based on relational concepts, and are appropriately called relational database management systems (or RDBMSs). All popular databases (including Oracle, MySQL, SQL Server, Sybase, Interbase, PostgreSQL, and DB2) are relational databases. Tomcat administrators must be well versed in RDBMSs. In addition, an understanding of the nature of interactions between an RDBMS and Tomcat is required to better anticipate the requirements that may arise. This chapter addresses the following topics: . Java Database Connectivity (JDBC), which is Java s database connectivity API . JDBC version evolution . JDBC driver types and advantages . Importance of connection pooling . Interactions between RDBMS and Tomcat . JNDI-based JDBC configurations . Standard configuration for a JDBC data source . Alternative JDBC configurations that may be required . Configuring alternative JDBC access This chapter also covers a variety of situations that may arise when configuring Tomcat to work with relational databases. The examples offered in this chapter feature hands-on configuration. Special emphasis is placed on the recommended or preferred way of interacting with databases. After reading this chapter, you will be comfortable with the integration of RDBMSs with Tomcat, and will be able to handle the most common requests for configuring RDBMSs to work with the Tomcat server.

For high quality java hosting services please check java web hosting website.

Chapter 12: Tomcat and IIS to Tomcat. The

Saturday, July 25th, 2009

Chapter 12: Tomcat and IIS to Tomcat. The requests that are not forwarded to Tomcat are served by the IIS Web server. Typically these would be requests for static resources, such as HTML files, images, and style sheets, as well as dynamic pages developed in Microsoft technologies such as ASP or C#. . Request for JSPs and servlets can be forwarded by the ISAPI plug-in to Tomcat using its Web application extension features. The specific instance of Tomcat that it forwards to is defined in the workers property file. . The ISAPI extension is responsible for gathering all request parameters as well as handling the response returned by Tomcat. This response is then returned to the user s Web browser. In addition, the chapter covered how to extend this setup for highly scalable Web deployment architectures, as well as listed troubleshooting tips for common configuration issues. The next chapter covers JDBC connectivity in Tomcat.

For high quality jboss hosting services please check jboss web hosting website.

Chapter 12: Tomcat and IIS 3. Modify the

Saturday, July 25th, 2009

Chapter 12: Tomcat and IIS 3. Modify the workers.propertiesfile. The workers.propertiesfile will have to be changed so that the host name (or IP address) of the Tomcat worker is the application server, as shown here: worker.myworker.host=myappserver All other entries remain the same. Note that the application server, myappserverin this example, must be IP addressable, from the Web server. This example of a distributed configuration of a server running IIS and another server running a single instance of Tomcat is a good demonstration of the client server concepts that form the foundation of the AJP 1.3 protocol. Multiple Tomcat Workers As shown earlier in the chapter, supporting multiple Tomcat workers requires the following steps: 1. Configure the AJP Connectors for the workers in Tomcat s server.xmlfile. 2. Add multiple workers to the workers property file as a comma-separated list, as shown: worker.list = my-tomcat-worker1, my-tomcat-worker2 worker.my-tomcat-worker1.type = ajp13 worker.my-tomcat-worker1.host = host1 worker.my-tomcat-worker1.port = 8009 worker.my-tomcat-worker2.type = ajp13 worker.my-tomcat-worker2.host = host2 worker.my-tomcat-worker2.port = 8009 The Tomcat workers can be running on separate hosts, as shown in the preceding code, or even on the same host. See Chapter 15 for information on how to run multiple Tomcat instances on the same host. Load-Balanced AJP Workers In addition to the AJP worker, a load-balanced worker consisting of multiple ajp13workers can be defined. Chapter 11 describes the details for setting up a load-balanced environment. The concepts discussed in Chapter 11 are specified for Apache and Tomcat, but are applicable regardless of the Web server used. More advanced load balancing configurations are covered in Chapter 17 . Summary This chapter presented details about using IIS as a Web front end to Tomcat. To conclude this chapter, let s review some of the key points discussed. The main focus of the chapter was configuring IIS to work as a Web front end to Tomcat using the ISAPI. As you saw, this works in the following way: . IIS is configured with the ISAPI plug-in, which works both as an ISAPI filter as well as a Web application extension. . The plug-in intercepts all requests to the IIS Web application, and then uses the URI-toTomcat- worker mappings defined in the uriworkermap file to decide which request to forward

For reliable and cheap web hosting services please check javaweb hosting website.