Comcast web hosting - JavaSercer Pages The interaction between the wrapper classes
JavaSercer Pages The interaction between the wrapper classes and a connection pool implementation is illustrated in Figure 17.2. Figure 17.2. A JDBC 1.0 connection pool wrapped with JDBC 2.0 interface classes Figure 17.2 can be explained like this: the application calls the DataSourceWrapper getConnection( ) method. The DataSourceWrapper obtains a Connection object from its ConnectionPool object. The ConnectionPool either finds an available Connection in its pool or creates a new one. The DataSourceWrapper creates a new ConnectionWrapper object for the Connection it obtained, and returns the ConnectionWrapper to the application. The application uses the ConnectionWrapper object as a regular Connection. The ConnectionWrapper relays all calls to the corresponding method in the Connection it wraps, except for the close( ) method. When the application calls the close( ) method, the ConnectionWrapper returns its Connection to the DataSourceWrapper, which in turn returns it to its ConnectionPool. In this example, I show you how to wrap the connection pool described in Jason Hunter and William Crawford’s Java Servlet Programming (O’Reilly). It’s a simple implementation, intended only to illustrate the principles of connection pooling. The source code for the connection pool is included with the code for this book, but I will not discuss the implementation of the pool itself, only how to make it look like a JDBC 2.0 connection pool. For production use, I recommend that instead of this code, you use a pool intended for real use, such as one of the implementations mentioned earlier. The first wrapper class is called com.ora.jsp.sql.ConnectionWrapper , shown in Example 17.1. Example 17.1. The ConnectionWrapper Class package com.ora.jsp.sql; import java.sql.*; import java.util.*; class ConnectionWrapper implements Connection { private Connection realConn; private DataSourceWrapper dsw; private boolean isClosed = false; public ConnectionWrapper(Connection realConn, DataSourceWrapper dsw) { this.realConn = realConn; this.dsw = dsw; } /** * Inform the DataSourceWrapper that the ConnectionWrapper * is closed. */ public void close( ) throws SQLException { isClosed = true; dsw.returnConnection(realConn); } /** * Returns true if the ConnectionWrapper is closed, false * otherwise. */ public boolean isClosed( ) throws SQLException { return isClosed; } /* * Wrapped methods. */ page 238
Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Sandzak.com j2ee web hosting services