Description

Tomcat generally uses the Apache DBCP component to provide pooling over JDBC drivers (org.apache.commons.dbcp).

Here is a sample of how to setup Tomcat to work with Attunity's JDBC driver and connection pooling. The sample was tested using Tomcat v4.1.

Solution


To setup Tomcat/DBCP to work with Attunity's JDBC driver perform the followings:

Add the following lines to the Tomcat's conf/server.xml file within the web application <Context> section:


<Resource name="jdbc/navdemo" auth="SERVLET" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/navdemo"> <parameter><name>driverClassName</name><value>com.attunity.jdbc.NvDriver</value></parameter> <parameter><name>url</name><value>jdbc:attconnect:127.0.0.1;DefTdpName=n avdemo</value></parameter>
</ResourceParams>


Here 'navdemo' is the database name (navdemo is a demo database that ships with Attunity Connect). The rest of the information is the class name for the Attunity driver as well as documented connection string (see the documentation for other connect string options).

In the webapps/<your-app>/WEB-INF/web.xml file add a reference to the resource defined above:


<resource-ref>
<res-ref-name>jdbc/navdemo</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Servlet</res-auth>
</resource-ref>


And then, a servlet could do something like:
__________________________________

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.NamingException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.*;
import java.sql.*;

public class JdbcServlet
extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
response.setContentType("text/plain");

Context ctx = null;

try {
ctx = new InitialContext();
} catch (NamingException e) {
out.println("Couldn't build an initial context : " + e);
return;
}

try {
out.println("Looking up java:comp/env/jdbc/navdemo resource...");
DataSource ds =
(DataSource) ctx.lookup("java:comp/env/jdbc/navdemo");
out.println("Getting connection...");
Connection conn = ds.getConnection();

if (conn == null) {
out.println("Failed to get connection");
return;
}
out.println("Got Connection "+conn.toString());
Statement stmt = conn.createStatement();
ResultSet rst =
stmt.executeQuery("select * from nation");
if (rst.next()) {
out.println("First data value: " + rst.getString(2));
}
conn.close();
} catch (Exception e) {
out.println("JDBC Exception occurred: " + e);
}
}
}

__________________________________

This servlet prints (using the default navdemo tables) the following text:
__________________________________
Looking up java:comp/env/jdbc/navdemo resource...
Getting connection...
Got Connection org.apache.commons.dbcp.PoolableConnection@1ce1bea
First data value: ALGERIA
__________________________________
It also verifies the use of connection pooling (the server connection is being maintained).