Defining data sources via JNDI has various benefits, e.g. pooling and completely external configuration. The latter is especially important as this avoids putting database credentials into your application. Since I’m currently studying the Spring framework, I also wanted to use another application server. In fact, I wanted to try Apache Tomcat 7, a servlet container.
I have to say that I found the JNDI how-to for Apache Tomcat insufficient and therefore want to share some information that I collected in addition to the how-to.
At first, they show what a context.xml file should look like but they don’t tell you where to put it (ok they lead you to another documentation page). I found the simplest place to be the $CATALINA_BASE/conf/context.xml as this is probably sufficient for most use cases. Please note though that all deployed applications will have access to this data source. The next thing you shouldn’t forget is to put the driver into $CATALINA_BASE/lib/. In combination with the aforementioned how-to this should be all you need to use JNDI data sources with Apache Tomcat.