How to use the Teradata JDBC Driver with Tomcat versions 5.5, 6.0, 7.0, 8.0, 8.5, and 9.0



Tomcat Data Source Definition

In Tomcat, a DataSource can be configured in a Context. A Context is an element in a XML file which represents a web application, which is running within a particular host.



Prerequisites

The following instructions assume that Tomcat is installed in the C:\Tomcat directory.

In Tomcat 5.5, terajdbc4.jar must be copied to the C:\Tomcat\common\lib directory. Beginning with Teradata JDBC Driver 16.20.00.11, tdgssconfig.jar is no longer used. With older versions of the Teradata JDBC Driver, tdgssconfig.jar must be copied to the C:\Tomcat\common\lib directory.

In Tomcat 6.0 and above, terajdbc4.jar must be copied to the C:\Tomcat\lib directory. Beginning with Teradata JDBC Driver 16.20.00.11, tdgssconfig.jar is no longer used. With older versions of the Teradata JDBC Driver, tdgssconfig.jar must be copied to the C:\Tomcat\lib directory.



Context Configuration

Context elements may be explicitly defined:

The following instructions assume the use of the recommended technique of using the main conf\context.xml.

The following Resource definition should be placed in between the <context>....</context> element in conf\context.xml.

<Resource name="jdbc/TeradataDS"
          auth="Container"
          type="javax.sql.DataSource"
          driverClassName="com.teradata.jdbc.TeraDriver"
          url="jdbc:teradata://whomooz"
          username="guest"
          password="please"
          maxActive="8"
          maxIdle="8"
          maxWait="-1" />


Description of <Resource> element attributes

Attribute

Description

Example

name

The name of the resource to be created, relative to the java:comp/env context. This must have prefix "jdbc/". jdbc/TeradataDS

auth

Specify whether the web Application code signs on to the corresponding resource manager programatically, or whether the Container will sign on to the resource manager on behalf of the application. The value of this attribute must be Application or Container. Container

type

The fully qualified Java class name expected by the web application when it performs a lookup for this resource. javax.sql.DataSource

driverClassName

The fully qualified Java class name of the Teradata JDBC Driver. com.teradata.jdbc.TeraDriver

url

The connection URL to be passed to Teradata JDBC Driver to establish a connection. jdbc:teradata://whomooz

UserName

The username for the database. guest

Password

A valid password for the user name. please

maxActive

The maximum number of active connections that can be allocated from this pool at the same time.

Note: Tomcat documentation indicates that zero or a negative number may be specified to indicate unlimited; however, there are reports that some versions of Tomcat may hang when zero is specified.
Default is 8

maxIdle

The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit. Default is 8

maxWait

The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or -1 to wait indefinitely. Default is -1

removeAbandoned

The removeAbandoned parameter controls Tomcat's behavior regarding orphaned connections. If set to true, a connection is considered abandoned and eligible to be reclaimed by the connection pool if it has been idle longer than the removeAbandonedTimeout. Setting this to true can recover connections from poorly written applications which fail to close a connection. Care must be taking when setting this to true, because Tomcat uses limited information in determining whether a connection is idle. For example, traversing a result set doesn't count as being used. Default is false


Accessing the Data Source from a Java Servlet

The following code creates the initial context:

Context ctx = new InitialContext();

To perform a JNDI lookup to obtain the Data Source, the following code can be used:

DataSource ds = (DataSource) ctx.lookup(jndiName);

Note: The jndiName must use prefix "java:comp/env/jdbc" .

To obtain a connection from the Data Source, the following code can be used:

Connection con = ds.getConnection();

At this point, the connection obtained behaves the same way as one that was obtained from the Driver Manager.

Note: The Datasource.getConnection(username,password) method is not supported by Tomcat 5.5 and above.

If this method is used, then the following exception will be thrown:

java.lang.UnsupportedOperationException: Not supported by BasicDataSource at
   org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:899)
   ...

Complete sample servlets are listed here.