Tomcat JNDI configuration for auto redeploy

With thanks to http://www.mail-archive.com/users@tomcat.apache.org/msg74521.html

Although it’s deprecated putting the context in server.xml seems to work quite nicely
however if you want to auto deploy e.g. on a CI server then you need to get a bit cleverer….

Don’t keep the application in the appBase – locate it outside of
Tomcat’s directory structure, and use the docBase attribute of the
element to tell Tomcat where to find it. Replace the .war file at your
leisure, and then do a touch of the .xml file to trigger an application redeployment

1) I have created a folder with absolute path:
##war directory##
2) In this folder I have placed my WAR file, ##Application##.war
3) In apache-tomcat-6.0.20/conf/Catalina/localhost
I have placed a file called ##Application.xml## (contents of file below).



<?xml version="1.0" encoding="UTF-8"?>

<Context docBase="##war directory##/##Application.war##">
<Resource name="jdbc/postgres" auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/XXXXXX"
username="YYYYYY" password="ZZZZZZ" maxActive="20"
maxIdle="10"
maxWait="-1"/>
</Context>

Fun with classloaders

Class loaders are one of those things which work most of the time and you don’t have to worry about but when they go wrong they can be a bit of a nuisance to sort out.

When you are running a java application in a application server there are several class loaders in play e.g. for tomcat see http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html
Most of the time this is what you want and any other behaviour would cause problems – think Singletons – however sometimes you are trying to be clever and using a method like Class.forName or ClassLoader.getResource in which case you need a better grasp on what is happening.

In this case you can use Thread.currentThread().getContextClassLoader() or Thread.currentThread().getClass().getClassLoader() as well as Class.getClassLoader to access the different class loaders and hopefully the class/resource which you are after.