How do you use a Tomcat JNDI JDBC datasource in Spring Boot

Zac Tolley picture Zac Tolley · Feb 28, 2014 · Viewed 23.4k times · Source

I have a Spring boot application and want to deploy as a WAR to Tomcat 7. As part of this I need to keep configuration out of the WAR, so that I can deploy the same war to my stage and production servers and have it pickup the mysql connection via configuration.

To this end I want to configure my Spring Boot app to use a mysql connection configured as a JNDI datasource in the Tomcat instance.

Can spring boot do this and if so how?

Alternatively is this easy to do in Spring 4 without resorting to xml based configuration.

Answer

dustin.schultz picture dustin.schultz · Mar 20, 2015

If you're using Spring Boot 1.2 or greater, this got easier. You can just add this to application.properties

spring.datasource.jndi-name=java:comp/env/jdbc/my_database

The relevant docs: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-connecting-to-a-jndi-datasource