I am working on a Java web application in Eclipse and deploying it to a Tomcat instance that runs Eclipse. I am trying to get this application to talk to a database on another host through a JNDI Resource element.
The context.xml file included in the application is trying to connect to the MySQL server running on the local host, for example:
<?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 correctly reads the environment values ββ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, for example:
... <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 sure my application uses JNDI correctly.
So now I want to deploy this to Tomcat on my desktop in Eclipse. I added the Tomcat project in a standard way, and I edited the context.xml file for the server (i.e. the one that Eclipse shows under my Tomcat server in the "Servers" project) as follows:
<?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 restart Tomcat in Eclipse, the log shows me that the path to the Log4J file is read correctly (because it starts to log in to the Tomcat console), and I even see that it spits out the correct "mykey" value. However, initialization fails when it tries to read from configDB .
At first I thought it was a connection problem, but after that I restarted Tomcat in the debugger and checked the DataSource read from JNDI; its URL points to localhost, i.e. it uses the one contained in the application context.xml file, not in Tomcat!
I tried several ways to get this working, including placing this information in a Tomcat server.xml file, but nothing works. Or, more precisely, this is a kind of work, because it reads the values ββof the environment, but not the resource.
What am I doing wrong here? How to force the Eclipse Tomcat server to override this resource?