A Simple Guide To Tomcat JDBC Resource Configuration

Java Database Connectivity, or JDBC, is a technology that allows Java technologies to connect to a wide variety of database types, over a single protocol, without altering the Java source code. This is accomplished by the use of a JDBC driver, a java class that is able to translate the Java byte code into the required database query syntax, and return parseable results. Tomcat uses JDBC to offer database connectivity to its hosted servlets and JSP pages.

In this article, we'll learn how to set up your database as a JDBC resource to be accessed on your Tomcat server, from choosing and installing the driver, to configuring connection pooling, to a comparison of the various methods of JDBC configuration that Tomcat supports.

Tip: Never configure JDBC twice! Tcat allows you to save common configurations and apply them to new instances or update the configuration for all instances with a single click. Click here to download free.

Step 1: Download and Install the Correct JDBC Driver

There are hundreds of database formats to choose from, and many of them provide not one, but two or three different JDBC driver types. When choosing a database, the stability and quality of the available JDBC drivers for a given solution should factor into your decision. Not all JDBC drivers are created equal.

JDBC drivers are classified into four groups by their functionality:

Type 1 - JDBC-ODBC Bridge

This driver is a platform dependent driver that uses JDBC to connect to another abstraction API, called ODBC, a Microsoft-developed database connectivity standard. Although widely supported, this is an inefficient driver due to the doubled transformation it must perform, and should be avoided if possible.

Type 2 - Native-API Driver

This driver uses a client-side, OS-specific native API to translate JDBC calls into a specific database format. Structurally, it functions in basically the same way (and has the same drawbacks) as a Type 1 driver, but is lighter in weight.

Type 3 - Network-Protocol Driver

Rather than using client or server-side transformation, this driver type, which is written in pure Java, forwards requests to a middleware server, which supports one or several different data formats. This abstraction layer eliminates any need to update the client itself with multiple database drivers. This server can also provide caching, load balancing, and so on, to increase the efficiency of this middle layer. This approach offers a very high degree of flexibility and stability, at the cost of some associated overhead.

Type 4 - Native-Protocol Driver

The simplest and most efficient of all the driver types, this is a driver written in pure java that performs a conversion of JDBC API calls to the necessary database format and directly connects to the database in question through a socket. As they are written in Java, these drivers are also platform independent, although a separate client-side driver is required for each type of database.

For most users, it will be best to use either a Type 3 or Type 4 JDBC driver, dependent on the complexity of the data transformations you will be performing.

comprehensive list of JDBC drivers is provided on Sun's website, and includes information about driver type, vendor, supported features, and where the driver can be downloaded.

Once you have obtained the correct driver, simply copy the driver's JAR file to your "$CATALINA_HOME/lib" directory and restart your server to make the driver available for use.

Step 2: Configure your Database as a JNDI Resource

After you have installed the appropriate driver, you'll need to make your database available to your application code by declaring it as a Resource. Tomcat uses the JNDI API to connect to JDBC-aware databases.

JNDI Resources can either be declared on a per-Context basis, or as Global Resources, accessible by multiple contexts through a Resource Reference. For example, you might declare a database of configuration settings for use with a specific context, and a general database of users as a Global Resource, to allow multiple applications to access the same login information.

The configuration process for these resources follows a basic shape across platforms, but can differ slightly by vendor. This database-type-specific information should be available on your vendor's website.

Our Step-By-Step Guide to Tomcat JDBC Configuration

Please visit our Tomcat MySQL Configuration article for an detailed guide to the JDBC configuration process, which you should find very useful regardless of the database format you are using. Configuring a different database flavor will only require you to purge from the basic steps we outline in small areas such as password format or port number.

In addition to covering both per-Context and global JDBC Resource configuration, we have provided extensive information about connection pooling, including a troubleshooting guide.