Thursday, September 9, 2010

Environment/Resource problems with Tomcat 7 in Eclipse

When developing JEE applications it is common practice (and very good practice in my humble opinion) to read in environment specific configurations from the container via JNDI.  Up until today I used Tomcat 6 with Eclipse and WST to develop on my local box.  I would edit the server.xml file to add my environment variables and my container managed database connections. The result looks somelthing like this:

...
<resource auth="Container" 
   driverclassname="com.mysql.jdbc.Driver"
   maxactive="100" maxidle="30"
   maxwait="10000" name="jdbc/mydb"
   password="root" type="javax.sql.DataSource"
   url="jdbc:mysql://localhost/mydb?autoReconnect=true"
   username="" validationquery="SELECT 1;" />
<environment name="var1" 
   type="java.lang.String" value="foo"/>
<environment name="var2" 
   type="java.lang.String" value="bar"/>
...

Today, I tried to move to using Tomcat 7--not for any particular reason except that I like to stay up on using the latest and greatest and since 7 > 6 it must be the latest; it remains to be seen whether or not it is the greatest.

When I tried to start using Tomcat 7 for local development I started by using the same setup I used for Tomcat 6 above. Everything looked good except that when I would try to start the server from the Servers tab, I would get weird NullPointerExceptions that had no apparent reason.






I tried taking the Resource and Environment stuff out of server.xml and the server would start up like a charm (except all the errors that it would throw because of my missing environment variables).

The fix is to double-click the Tomcat 7 server in the Servers tab and check the box next to Publish module contexts to separate XML files in the Server Options section.










Apparently, the startup order has changed between Tomcat 6 and Tomcat 7 causing some unhappiness.  I found the solution at the Eclipse Community Forum.