Spring DataSource JNDI with Tomcat Example

Welcome to the Spring DataSource JNDI Tomcat Example Tutorial. Earlier we saw how to implement database operations using Spring JDBC integration. However most of the time enterprise applications are deployed in a servlet container such as Tomcat, JBoss etc.

Spring DataSource

We know that DataSource with JNDI is the preferred way to achieve connection pooling and get benefits of container implementations. Today we will look how we can configure a Spring Web Application to use JNDI connections provided by Tomcat.

For my example, I will use MySQL database server and create a simple table with some rows. We will create a Spring Rest web service that will return the JSON response with list of all the data in the table.

Database Setup

Spring DataSource MVC Project

Create a Spring MVC Project in the Spring Tool Suite so that our spring application skeleton code is ready. Once we will be done with our implementation, our project structure will look like below image.


Spring JDBC and Jackson Dependencies

We will have to add Spring JDBC, Jackson and MySQL Database driver as the dependencies in the pom.xml file. My final pom.xml file looks like below.

If you are not familiar with Rest in Spring, please read Spring Restful Webservice Example.

Model Class

Our Employee bean that is modeled after Employee table, looks like below.

Spring Controller Class

Our simple controller class looks like below.

Important Points about the Controller class are:

  • DataSource will be wired by Spring Bean configuration with name dbDataSource.
  • We are using JdbcTemplate to avoid common errors such as resource leak and remove JDBC boiler plate code.
  • URI to retrieve the list of Employee will be https://{host}:{port}/SpringDataSource/rest/emps
  • We are using @ResponseBody to send the list of Employee objects as response, Spring will take care of converting it to JSON.

Spring Bean Configuration

There are two ways through which we can JNDI lookup and wire it to the Controller DataSource, my spring bean configuration file contains both of them but one of them is commented. You can switch between these and the response will be the same.

  1. Using jee namespace tag to perform the JNDI lookup and configure it as a Spring Bean. We also need to include jee namespace and schema definition in this case.
  2. Creating a bean of type org.springframework.jndi.JndiObjectFactoryBean by passing the JNDI context name. jndiName is a required parameter for this configuration.

My spring bean configuration file looks like below.

Tomcat DataSource JNDI Configuration

Now that we are done with our project, the final part is to do the JNDI configuration in Tomcat container to create the JNDI resource.

Add above configuration in the GlobalNamingResources section of the server.xml file.

We also need to create the Resource Link to use the JNDI configuration in our application, best way to add it in the server context.xml file.

Notice that ResourceLink name should be matching with the JNDI context name we are using in our application. Also make sure MySQL jar is present in the tomcat lib directory, otherwise tomcat will not be able to create the MySQL database connection pool.

Running the Spring DataSource JNDI Sample Project

Our project and server configuration is done and we are ready to test it. Export the project as WAR file and place it in the tomcat deployment directory.

The JSON response for the Rest call is shown in the below image.


That’s all for the Spring integration with servlet container JNDI context, download the sample project from the below link and play around with it to learn more.

By admin

Leave a Reply