Search code examples
spring-boottomcatspring-dataspring-data-jdbc

How to configure JNDI Datasource with embedded Tomcat for Spring Data JDBC using Spring Boot 3.1.1 that will also deploy to external Tomcat?


So I was curious if anyone has a pattern out there to solve this with a recent version of Spring Boot? My IT Department hosts their own Tomcat servers and prior to me switching over to Spring Boot we would create our own DataSource configuration class to get the proper database connection information from the servers Context.xml because IT managed their own database credentials and just gave us the JNDI path. Then when developing locally would use a local Context.XML to do the same thing with the same JNDI path but using my issued credentials. Can I do this with embedded tomcat so when I deploy to Dev and Production servers managed by IT that it will connect without me having to change anything? I am using application.yml file to configure spring=>datasource=>jndi-name.

Thanks


Solution

  • This is a perfect use case for Spring Profiles.

    You can work with profiles in many ways. See this article for an overview. For your use case the following is probably sufficient.

    1. Create the production application.properties with the configuration for the JNDI datasource as normal.
    2. Create a copy of that file as application-dev.properties, and replace the datasource configuration with the configuration of your local datasource.
    3. Configure your development environment to set spring.profiles.active=dev

    This is essentially the comment of M. Deinum. So if you find it useful find some nice answer of him and give it an upvote. Shouldn't be difficult to find a good one.