Product: | JDBC DBConnectivity Products (HiT JDBC/400, HiT JDBC/DB2) |
Version: | All |
ID: | 1591 |
Summary: | Using JDBC data connectors with Apache Tomcat |
This document describes how to define and use the HiT JDBC Driver as a datasource in a Tomcat environment. This document assumes that you are familiar with how to create and deploy web applications such as JSPs and Servlets in the Tomcat environment and that you have already set up the server and are able to run sample applications provided by Tomcat.
Steps:
- Copy all the HiT JDBC driver jar files, including the license jar file, to the directory $CATALINA_HOME/common/lib.
- Edit the file $CATALINA_HOME/conf/Catalina/localhost/<myapp>.xml to add the JNDI datasource definition for your application, where <myapp> is the name of your application.
The following example shows the definition for a JNDI JDBC datasource named “jdbc/myds1” in the webapp “jsp-examples”. “jsp-examples” is one of the sample webapps provided with Tomcat. The datasource is a HiT/JDBC DB2 datasource and configured for connection pooling using DBCP provided by Tomcat. (See DBCP documentation for additional information.)
The file $CATALINA_HOME/conf/Catalina/localhost/jsp-examples.xml should look something like this:
<?xml version='1.0' encoding='utf-8'?>
<Context displayName="JSP 2.0 Examples" docBase="C:\devel\jtomcat5028\webapps\jsp-examples" path="/jsp-examples" workDir="work\Catalina\localhost\jsp-examples">
<Environment name="foo/name4" type="java.lang.Integer" value="10"/>
<Environment name="minExemptions" type="java.lang.Integer" value="1"/>
<Environment name="foo/bar/name2" type="java.lang.Boolean" value="true"/>
<Environment name="name3" type="java.lang.Integer" value="1"/>
<Environment name="foo/name1" type="java.lang.String" value="value1"/>
<Resource auth="Container" description="My Test Data Source 1" name="jdbc/myds1" type="javax.sql.DataSource"/>
<Resource name="jdbc/myds1" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/myds1">
<parameter>
<name>url</name>
<value>jdbc:db2://mydbserver:50000/sampldb1</value>
</parameter>
<parameter>
<name>validationQuery</name>
<value>select count(*) from sysibm.sysdummy1</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>hit.db2.Db2Driver</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>username</name>
<value>johnm</value>
</parameter>
<parameter>
<name>password</name>
<value>johnmpwd</value>
</parameter>
</ResourceParams>
</Context>
- The datasource is now ready for use by the application. The code to retrieve and use a connection from the datasource should look something like this:
… Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/myds1"); Connection conn = ds.getConnection(); … Statement stmt = conn.createStatement(); ResultSet resultSet = stmt.executeQuery(“SELECT * FROM TESTTABLE”); … stmt.close(); conn.close(); …