0% found this document useful (0 votes)
80 views2 pages

Connector/J With Tomcat: .Jar $CATALINA - HOME/common/lib

This document provides instructions for configuring a JNDI DataSource in Tomcat to connect to a MySQL database using Connector/J. It describes: 1) Installing the Connector/J JAR file in Tomcat's lib directory. 2) Configuring the DataSource by adding a <Resource> element to the server.xml file and nested <ResourceParams> to define connection properties like the username, password, URL, and driver class. 3) An alternative validationQuery value of /* ping */ that sends a ping to the server instead of the default SELECT 1 query.

Uploaded by

sandeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views2 pages

Connector/J With Tomcat: .Jar $CATALINA - HOME/common/lib

This document provides instructions for configuring a JNDI DataSource in Tomcat to connect to a MySQL database using Connector/J. It describes: 1) Installing the Connector/J JAR file in Tomcat's lib directory. 2) Configuring the DataSource by adding a <Resource> element to the server.xml file and nested <ResourceParams> to define connection properties like the username, password, URL, and driver class. 3) An alternative validationQuery value of /* ping */ that sends a ping to the server instead of the default SELECT 1 query.

Uploaded by

sandeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Connector/J with Tomcat

The following instructions are based on the instructions for Tomcat-5.x, available
at http://tomcat.apache.org/tomcat-5.5-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.
Next, configure the JNDI DataSource by adding a declaration resource
to $CATALINA_HOME/conf/server.xml in the context that defines your web application:
<Context ....>

...

<Resource name="jdbc/MySQLDB"
auth="Container"
type="javax.sql.DataSource"/>

<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>

<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.cj.jdbc.Driver</value>
</parameter>

<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/test</value>
</parameter>

</ResourceParams>
</Context>
Connector/J introduces a facility whereby, rather than use a validationQuery value of SELECT
1, it is possible to use validationQuery with a value set to /* ping */. This sends a ping to
the server which then returns a fake result set. This is a lighter weight solution. It also has the
advantage that if using ReplicationConnection or LoadBalancedConnection type
connections, the ping will be sent across all active connections. The following XML snippet
illustrates how to select this option:
<parameter>
<name>validationQuery</name>
<value>/* ping */</value>
</parameter>
Note that /* ping */ has to be specified exactly.
In general, 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 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
Note that the auto-loading of drivers having the META-INF/service/java.sql.Driver class in
JDBC 4.0 and later causes an improper undeployment of the Connector/J driver in Tomcat on
Windows. Namely, the Connector/J jar remains locked. This is an initialization problem that is not
related to the driver. The possible workarounds, if viable, are as follows: use
"antiResourceLocking=true" as a Tomcat Context attribute, or remove the META-
INF/ directory.
s

You might also like