Eclipse: overriding JNDI resource in Tomcat

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?

+6
source share
1 answer

The main problem is that the context.xml you are editing in eclipse matches the main context.xml in the conf tomcat directory. And then preferences in the application context file take precedence. As you already described in the normal tomcat deployment lifecycle, this is great because you are simply editing application settings.

You can do the same with the context file created by eclipse (which is located somewhere in .metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf/Catalina/localhost ), but it will be overwritten as only you will change something in the original context file ( META-INF/context.xml ). Therefore, in fact, you will need to make changes there, but this, as a rule, is not in doubt, because this file must be deployed without user changes.

But here is a workaround: you can force eclipse to use a different META-INF/context.xml , abusing the deployment assembly. Create a new META-INF somewhere else and place the context.xml file with the settings there. Then, in the project properties of your eclipse project in the Deployment Assembly add an additional mapping from your new path/to/META-INF to META-INF .
Then eclipse will overwrite the original context.xml its user, and tomcat should complete your settings.

+1
source

All Articles