Wednesday, January 9, 2013

Datasource not updated from context.xml in Tomcat

Today while working on a project I encountered an interesting problem. The project I am working on is basically a web application which behaves as a server for a J2ME mobile applicaton and consists of a bunch of application servlets that handle requests from a mobile application. The database is MySQL 5.1 and I am using plain and simple JDBC, no OR mapping and fancy stuff like Hibernate. My application's context.xml is the place where I store connections looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/ABCSchool">

<!-- Development Environment -->
<Resource name="jdbc/myDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="myproject" password="mypassword"    driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://remoteserver:3306/projectdb". />

The context.xml file is located under Configuration Files in a Netbeans Web Application project. After the project is built and cleaned, the WAR file produced would contain 'context.xml' in META-INF folder in the root directory of your project. When I deployed the application I realized that I have given the wrong database since the mobile application could not connect. I tried the following, all of which DID NOT work:
  1. Changed the Resource tag in the context.xml file after stopping the Tomcat sever 
  2. Modified context.xml after stopping Tomcat and restarting it
  3. Removed WAR file and deleted application directory. Deployed a fresh copy of my application with the correct database URL in the context.xml but it still did not work!
  4. Cleaned the contents of the 'work' directory and repeated step 2 & 3. No luck.
Turned out I was editing the wrong file (thanks to Mark Thomas's reply here). Tomcat copies the context.xml file from the META-INF folder and copies to its 'conf' folder. You can find it under:


(usually CATALINA_BASE/conf/Catalina/localhost)

My CATALINA_BASE if for instance /var/lib/tomcat6. Every time a project is deployed for the first time, Tomcat copies the context.xml to an XML file named after the application's name eg. ProjectABCSchool's context.xml would be copied to a file called ProjectABCSchool.xml in the 'conf' folder.

Hope this helps anyone stuck with the similar problem!


No comments:

Post a Comment