27.3.4.2. Using Connector/J With Tomcat
27.3.4.2. Using Connector/J With Tomcat
27.3.4.2. Using Connector/J With Tomcat
The following instructions are based on the instructions for Tomcat-5.x, available at
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html which is
current at the time this document was written.
First, install the .jar file that comes with Connector/J in $CATALINA_HOME/common/lib so that it
is available to all applications installed in the container.
<Context ....>
...
<Resource name="jdbc/MySQLDB"
auth="Container"
type="javax.sql.DataSource"/>
<!-- The name you used above, must match _exactly_ here!
The connection pool will be bound into JNDI with the name
"java:/comp/env/jdbc/MySQLDB"
-->
<ResourceParams name="jdbc/MySQLDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>validationQuery</name>
<value>SELECT 1</value>
</parameter>
<!-- The most conservative approach is to test connections
before they're given to your application. For most applications
this is okay, the query used above is very small and takes
no real server resources to process, other than the time used
to traverse the network.
<parameter>
<name>testOnBorrow</name>
<value>true</value>
</parameter>
<parameter>
<name>testWhileIdle</name>
<value>true</value>
</parameter>
<parameter>
<name>timeBetweenEvictionRunsMillis</name>
<value>10000</value>
</parameter>
<parameter>
<name>minEvictableIdleTimeMillis</name>
<value>60000</value>
</parameter>
<parameter>
<name>username</name>
<value>someuser</value>
</parameter>
<parameter>
<name>password</name>
<value>somepass</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/test</value>
</parameter>
</ResourceParams>
</Context>
In general, you should follow the installation instructions that come with your version of Tomcat,
as the way you configure datasources in Tomcat changes from time-to-time, and unfortunately if
you use the wrong syntax in your XML file, you will most likely end up with an exception
similar to the following:
Error: java.sql.SQLException: Cannot load JDBC driver class 'null ' SQL
state: null
Previous / Next / Up / Table of Contents
User Comments
Posted by Al Hopper on November 18 2005 1:32pm [Delete] [Edit]
The above did not work for me with Tomcat 5.5.9, but the following did:
server.xml is the old location for the <Context> but these days it's usually found either in
conf/Catalina/[hostname]/[appname].xml or in webapps/[appname]/META-INF/context.xml.
Also note that the syntax of <Resource> has changed in 5.5. See
http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
Note, the <ResourceParams> tag used in the above example is no longer valid Tomcat 5.5; it's
still OK for tomcat 5.0 and lower.
The example above is not useful to newbies for several reasons. It assumes a newbie already
understands the concept of declaring a resource within a context for a web application. It also
assumes that the newbie wants to declare this database connection resource to be global across
all web applications in this Tomcat installation, but doesn't say that.
Here's what worked for me.
I then added the following for the content of context.xml (from <Context> to </Context> tag):
</Context>
Next I placed the following into a .jsp file in the MyApps directory:
<%@ page
import="java.sql.*"
import="java.util.*"
import="java.io.*"
import="org.apache.commons.dbcp.*"
import ="org.apache.commons.pool.*"
import="org.apache.commons.pool.impl.*"
import="javax.naming.*"
import="javax.sql.*"
%>
<%
DataSource ds=null;
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
if(ds == null )
throw new Exception("Boom - No Datasource");
Connection conn=ds.getConnection();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select 'Hello World from MySQL'");
if (rs.next()){
%><%=rs.getString(1)%><%
}
%>
Note that "jdbc/TestDB" is the key string used to access the database connection resource info in
the context.xml file.
You can see my overly detailed writeup on installing Java, Tomcat, and MySQL into an Mac OS
X environment (pretty close to generic Unix) at the web address:
http://www.benslade.com/projects/java/tomcat/InstallingJavaTomcatMySQLOnMacOSX/
Ben Slade
PublicMailbox at BenSlade dot com
NB: AutoReconnect=true is deprecated. See changelog section 23.3.6. (go several pages forward
to the Changelog)