I'm working on a Java web application in Eclipse and deploying to an instance of Tomcat that is run by Eclipse. I'm trying to get this application to talk to a database on another host via a JNDI Resource
element.
The context.xml file included in the application attempts to connect to a MySQL server running on localhost, like so:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Environment name="log4j.configuration"
value="/path/to/installed/log4j.properties"
type="java.lang.String" />
<Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/dbname" maxActive="8"
validationQuery="SELECT 1" testOnBorrow="true" />
<Environment name="mykey" value="installed" type="java.lang.String" />
</Context>
This configuration works when I install it on the actual host. That is, it reads the environment values correctly and connects to the database.
I can also install this on a separate host and edit /etc/tomcat6/conf/Catalina/localhost/myaplication.xml
to change the JDBC URL, like so:
...
<Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://otherhost:3306/dbname" maxActive="8"
validationQuery="SELECT 1" testOnBorrow="true" />
...
This configuration also works. In other words, I'm pretty confident that my application is using the JNDI correctly.
So, now I want to deploy this to Tomcat on my desktop in Eclipse. I've added the project to Tomcat in the standard way, and I've edited the context.xml file for the server (i.e., the one that Eclipse shows under my Tomcat server in the "Servers" project) like so:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Environment name="log4j.configuration"
value=C:\workspace\myapplication\src\main\resources\conf\log_to_tomcat_console_log4j.properties"
type="java.lang.String" />
<Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://otherhost:3306/dbname" maxActive="8"
validationQuery="SELECT 1" testOnBorrow="true" />
<Environment name="mykey" value="desktop" type="java.lang.String" />
</Context>
When I then restart Tomcat in Eclipse, log shows me that the path to the Log4J file is being read correctly (because it starts logging to the Tomcat console) and I even see it spit out the correct "mykey" value. However, initialisation fails when it tries to read from the configDB
.
At first, I thought this was a connectivity issue, but after ruling that out, I restarted Tomcat in the debugger and inspected the DataSource read from JNDI; its URL points to localhost, meaning it's using the one in the application's context.xml file, not Tomcat's!
I've tried several ways to get this working, including putting this information in Tomcat's server.xml file, but nothing seems to work. Or, more accurately, it sort of kind of works because it's reading the Environment values but not the Resource.
What am I doing wrong here? How do I get Eclipse's Tomcat server to override this Resource?