87 lines
8.4 KiB
HTML
87 lines
8.4 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||
|
<html>
|
||
|
<head>
|
||
|
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
|
||
|
|
||
|
<title>Access connection pools from your application components</title>
|
||
|
</head>
|
||
|
|
||
|
<BODY>
|
||
|
<!-- Java sync-link -->
|
||
|
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
|
||
|
|
||
|
<h5><a name="datapacc"></a>Access connection pools from your application components</h5>
|
||
|
|
||
|
<p>This topic describes how to access connection pools from application components such as servlets and JavaServer Pages (JSP) files. This includes instructions for obtaining a connection from a connection pool and returning that connection back to the pool once the application component is finished using it.</p>
|
||
|
|
||
|
<p>Connection pooling is defined as part of the JDBC 2.0 Optional Package. Another part of the Optional Package provides for the use of the Java Naming and Directory Interface (JNDI) and DataSource objects to access relational databases. IBM has implemented these extensions in WebSphere Application Server - Express. These extensions are explained using code fragments that show how relational databases are accessed by application components using the JDBC 2.0 Optional Package.</p>
|
||
|
|
||
|
<p><a href="http://java.sun.com/products/jdbc/" target="_">JDBC 2.0</a> <img src="www.gif" width="18" height="15" alt="Link outside Information Center"> defines the Java APIs for access to relational databases. With the introduction of JDBC 2.0, the APIs have been split into two parts:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li><p><strong><a href="http://java.sun.com/j2se/1.3/docs/api/java/sql/package-summary.html" target="_">The JDBC 2.0 Core API</a></strong> <img src="www.gif" width="18" height="15" alt="Link outside Information Center">
|
||
|
<br>The Core API contains evolutionary improvements, but has been kept small and focused in order to promote ease of use. The 2.0 API classes and interfaces are located in the java.sql package that is shipped with each JDBC driver. You can use these classes and interfaces in your application components by using this import statement: <tt>import java.sql.*;</tt></p></li>
|
||
|
|
||
|
<li><p><strong><a href="http://java.sun.com/products/jdbc/jdbc20.stdext.javadoc/" target="_">The JDBC 2.0 Optional Package</a></strong> <img src="www.gif" width="18" height="15" alt="Link outside Information Center">
|
||
|
<br>The Optional Package defines specific kinds of additional functionality when vendors are ready to provide the functionality and programmers are ready to use the functionality. WebSphere Application Server - Express supports the JDBC 2.0 Optional Package and implements connection pooling. Therefore, application components can be coded to make efficient use of database connection resources.</p>
|
||
|
|
||
|
<p>The JDBC 2.0 Optional Package classes and interfaces are located in the javax.sql package that is shipped with each JDBC driver. You can use these classes and interfaces in your application components by using this import statement: <tt>import javax.sql.*;</tt></p></li>
|
||
|
</ul>
|
||
|
|
||
|
<p>WebSphere Application Server - Express uses JNDI caching. This function has implications on the results received from JNDI lookups. Because data sources are generally static, this should not affect most application components using connection pooling.</p>
|
||
|
|
||
|
<p>Connection pooling can be used by application components such as servlets and JSP files.</p>
|
||
|
|
||
|
<p>To obtain a connection in your application components, you must obtain a connection from a connection pool using a data source object, use that connection, and then return the connection back to the connection pool. These coding concepts are shown in the code fragments in these steps:</p>
|
||
|
|
||
|
<ol>
|
||
|
<li><p>Make sure that j2ee.jar is specified in your classpath. These files are necessary to import the packages mentioned in the next step and to successfully compile your application component. These files are in the /QIBM/ProdData/WebASE51/ASE/lib directory.</p></li>
|
||
|
|
||
|
<li><p>Import JDBC packages and IBM implemented extensions. Import the JDBC 2.0 Core classes, the IBM implementation of the JDBC 2.0 Optional Package classes, and the JNDI naming classes. These are the classes needed for database access:</p>
|
||
|
|
||
|
<pre>import java.sql.*;
|
||
|
import javax.sql.*;
|
||
|
import javax.naming.*;</pre>
|
||
|
|
||
|
<p>Additionally, import the application component specific packages required for the application components that you are writing. For example, import the following packages if you are writing a servlet application component:</p>
|
||
|
|
||
|
<pre>import javax.servlet.*;
|
||
|
import javax.servlet.http.*;</pre></li>
|
||
|
|
||
|
<li><p>Using JNDI, obtain the initial JNDI context for the naming service. (For more information, see <a href="jndicntx.htm">Obtain the initial JNDI context for the component</a>.) JNDI provides a way to store objects and files (such as DataSource objects) as well as a way to retrieve them. The initial JNDI context is used to access the JNDI naming server to retrieve a reference to a DataSource object.</p></li>
|
||
|
|
||
|
|
||
|
<li><p>Using the initial JNDI context, look up a data source object. (For more information, see <a href="jndilkup.htm">Use JNDI to look up Java components</a>.) The DataSource object is defined in the JDBC 2.0 Optional Package. The actual name to look up is defined by the DataSource object. See <a href="datacfg.htm">Configure WebSphere Application Server - Express to access databases</a> for information on configuring a data source and its JDBC provider using the WebSphere Application Server administrative console.</p>
|
||
|
|
||
|
<p>DataSource objects are stored in the <tt>jdbc</tt> context. For example, if you have a DataSource named <tt>AccountDataSource</tt>, the JNDI name for that DataSource would be <tt>jdbc/AccountDataSource</tt>.</p>
|
||
|
|
||
|
<p><strong>Note:</strong> The JNDI name can be read from an external property file or application environment variable, making the application component more portable.</p></li>
|
||
|
|
||
|
<li><p>The DataSource object is a <strong>factory</strong> that you use to get an actual JDBC Connection object. For each client request made to the application component, a connection is retrieved from the DataSource object. For example:</p>
|
||
|
|
||
|
<pre>conn = ds.getConnection();</pre>
|
||
|
|
||
|
<p><strong>Note:</strong> Obtaining a connection from the DataSource object retrieves an available connection from the connection pool that is associated with the DataSource object.</p></li>
|
||
|
|
||
|
<li><p>Use the Connection object to create one or more Statement objects. Queries and updates use Statement objects to perform SQL interactions with the database. The JDBC 2.0 Core API provides classes and interfaces for accessing relational database. For example, you could use a Statement object to run an SQL SELECT statement against a relational database file that returns its results in the form of a ResultSet object.</p></li>
|
||
|
|
||
|
<li><p>Close all result set and statement objects that were used by the connection:</p>
|
||
|
<pre>myResultSet.close()
|
||
|
myStatement.close()</pre>
|
||
|
|
||
|
<p><strong>Note:</strong> Implicitly, these objects are closed when the connection is closed, but explicitly closing them can improve performance and can eliminate JDBC timing issues that can cause memory leaks in your application components.</p></li>
|
||
|
|
||
|
<li><p>At the end of each client request, free the connection resource:</p>
|
||
|
|
||
|
<pre>conn.close()</pre>
|
||
|
|
||
|
<p><strong>Note:</strong> The Connection object is not actually closed when the close() method is invoked on it, so there is no close-related overhead. Instead, the Connection object is returned to the connection pool for reuse. In a similar fashion, the earlier getConnection() method on the DataSource object did not incur the overhead of creating a new Connection object, but instead returned an existing Connection object from the connection pool. (A new Connection object is created only if the connection pool does not have an available Connection object.)</p>
|
||
|
|
||
|
<p>In some application environments or with some data sources, there might be no automatic connection pooling mechanism associated with a DataSource object. Thus, every getConnection() method call on a DataSource object and the related close() operation can in fact incur the overhead of creating a new connection and closing that connection. Within WebSphere Application Server - Express, however, using a DataSource object to get a Connection object automatically provides the efficiencies of connection pooling.</p></li>
|
||
|
</ol>
|
||
|
|
||
|
</body>
|
||
|
</html>
|
||
|
|