aspose file tools*
The moose likes Tomcat and the fly likes Configuring a MySQL DataSource in Tomcat5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Configuring a MySQL DataSource in Tomcat5" Watch "Configuring a MySQL DataSource in Tomcat5" New topic
Author

Configuring a MySQL DataSource in Tomcat5

Edward Kenworthy
Ranch Hand

Joined: Oct 05, 2003
Posts: 66
I'm trying to configure a DataSource but I'm getting this exception:

java.sql.SQLException: Cannot load JDBC driver class 'null'
org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:529)
org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:312)
info.kenworthy.reqxml.webapp.servlets.ManageProjects.doGet(Unknown Source)
javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
info.kenworthy.reqxml.webapp.servlets.Controller.doGet(Unknown Source)
javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

Which is thrown by this bit of code in my Servlet:

conn = _pool.getConnection();

I have the DS resource configured in my web.xml like this:


<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/ReqXMLDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

And in the Tomcat server.xml inside the localhost <Host> like this:

<Context path="/ReqXML" docBase="ReqXML"
debug="5" reloadable="true" crossContext="true">

<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>

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

<ResourceParams name="jdbc/ReqXML">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->

<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>

<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>

<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>

<!-- MySQL dB username and password for dB connections -->

<parameter>
<name>username</name>
<value>reqxml</value>
</parameter>
<parameter>
<name>password</name>
<value>reqxml</value>
</parameter>

<!-- Class name for the official MySQL Connector/J driver -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>

<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/reqxml?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>

And I've copied mysql-connector-java-3.0.14-production-bin.jar into my Tomcat common/lib directory.

Help!
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

the filename you mention, mysql-connector-java-3.0.14-production-bin.jar sounds like a download jar? If you open this file, do you see another connectorj jar file inside of it? If so, then you'll need to extract it into common/lib (and remove the parent jar).

err... but more likely:

The names don't match.
[ June 02, 2004: Message edited by: Mike Curwen ]
Edward Kenworthy
Ranch Hand

Joined: Oct 05, 2003
Posts: 66
OK looks like the problem is a bug in Tomcat. I haven't been able to nail it down completely but basically once you have configured a DataSource you *must* restart Tomcat everytime you re-deploy a WebApp that uses that DataSource- simple undeploying then deploying using the Tomcat ant tasks doesn't work.

This seems to be part of a general problem with Tomcat 5 not undeploying/deploying WebApps properly. I've had similar problems with jsp files where it "find" the jsp but then can't fine the translated (xxx_jsp.java) version after a re-deploy. Both these errors are intermittent: Restarting (and sometimes even re-booting my Mac!) is required to fix it.

Edward
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Configuring a MySQL DataSource in Tomcat5