Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tomcat 5 and context.xml problem

Status
Not open for further replies.

FatalErrorGuy

Programmer
Nov 17, 2005
4
US
Hi everyone,
I'm trying to get Tomcat 5.0.28 to work with mysql. It works fine if I define the datasource in the actual JSP like this:
<sql:setDataSource var="ADataSource"
url="jdbc:mysql://localhost:3306/javatest" driver="com.mysql.jdbc.Driver" user="myusername" password="myusername"/>

However, I can't for the life of me get it to work by defining the datasource in a context.xml. I made a subdirectory called META-INF and put a context.xml file in there with the following:

<Context path="/" docBase="webapps/ROOT" debug="1" reloadable="true">

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

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

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

<parameter>
<name>username</name>
<value>myusername</value>
</parameter>
<parameter>
<name>password</name>
<value>mypassword</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name> <value>jdbc:mysql://localhost:3306/javatest</value>
</parameter>
</ResourceParams>
</Context>

I keep getting this error:
javax.servlet.ServletException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver"

I've tried putting the context.xml file under a subdirectory called META-INF, as well as in my $CATALINA_HOME/conf/Catalina/localhost/ directory as a file called test1c.xml (since the file is test1c.jsp).

I've tried playing with the docBase settings,etc.

Right now my goal is to figure out some way to know for certain if Tomcat is recognizing my context.xml. For example, I'd like to make a JSP that displays a value from the context.xml file so I can tell if it's working.

Has anyone had trouble getting context.xml to work?
Does anyone know how to display a Resource value from context.xml?

Thank you so much in advance!!
Mike
 
Did you put the mysql driver jar file in TOMCAT_HOME/common/lib ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hi Sedj,
Thanks for replying!

Yes I have mysql-connector-java-3.1.11-bin.jar in common/lib. In fact, I know Tomcat can talk to mysql because I'm able to connect to mysql when I define the datasource within the JSP. So the problem I'm having is with either (a) getting Tomcat to recognize my context.xml file or (b) properly setting up the context.xml file. I just can't figure out path to pursue to pinpoint the problem.
 
Oh, OK. Its just from the error, "java.sql.SQLException: No suitable driver" you'd think it was a classpath issue !

I have to admit I never did get DBCP to work in Tomcat, sorry I can't be more help.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hi Sedj-
I wish I had a way of seeing which files Tomcat can currently see. Thanks for your help. :)
 
So I was able to resolve my problem. Here's what worked for me in case it'll help anyone else:

I was developing on a production machine. For whatever reason, Tomcat didn't seem to recognize my META-INF/context.xml file (located at /usr/local/tomcat/webapps/mysqltest/META-INF/context.xml). Here's what ultimately worked.

My JSP is called test.jsp, and it's located in:
/usr/local/tomcat/webapps/mysqltest/test.jsp
I took my context.xml file, renamed it to mysqltest.xml and moved it to
/usr/local/tomcat/conf/Catalina/localhost/mysqltest.xml

Then I changed the first line of mysqltest.xml to:
<Context path="/mysqltest" docBase="/usr/local/tomcat/webapps/mysqltest" debug="1" reloadable="true">

It also worked if I used the following relative path:
<Context path="/mysqltest" docBase="mysqltest" debug="1" reloadable="true">

The docBase path can be a full path, or it can be relative to webapps directory. But it did NOT work when I included the slash at the beginning of the docBase like such:
docBase="/mysqltest"

But I still have NO idea when you're supposed to use META-INF/context.xml
Does anyone know what that's for? Is it a file that is only used if you're created a WAR?

Good luck to everyone else! And thanks again, Sedj, for replying
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top