Configuring JNDI Data Source for Database Connection Pooling in Tomcat?

By | March 8, 2022

In this post, we will learn how to create a JNDI(Java Naming and Directory Interface) resource that represents a JDBC Data Source in Tomcat, and then how to configure a Java web application in order to access the JNDI Data Source. The benefits of using a JNDI Data Source are:

  • It uses database connection pooling services provided by the container, i.e. Tomcat uses Commons DBCP (tomcat-dbcp.jar) and Commons Pool as the implementation.
  • It externalizes database connections and makes it independent from the web application.
  • It shares database connections across applications deployed in the Web container.

The following examples are tested in Tomcat 9 and MySQL Database 8

Project Structure using Tomcat Database Connection Pool:

NOTE: This web Application requires three jars jstl-1.2.jar,mysql-connector-java-8.0.28.jar &servlet-api-2.5.jar  jars in WEB-INF/lib directory 

1. Sample MySQL database

First, we need to create a sample database. Let’s execute the following MySQL script(Bookstore.sql):

This DB script will create a database schema is called ‘jdbcpooldb’ and a table called book_table.  Here we are inserting some dummy data into this table using INSERT queries.

To interact with the MySQL database from the Java Web application, the MySQL Connector/J library must be present in the classpath. Here, we need to copy the mysql-connector-java-8.0.28.jar (or choose the correct version for your application) jar file to the application/lib directory.

2. Configure context

To declare a JNDI Data Source for the MySQL database, create a  Resource XML element with the following content:

Add above element inside the root element <Context> in a context.xml file. There are a couple of places where the context.xml XML file can be resided (create one if not exist):

  • Inside /META-INFdirectory of a web application: the JNDI Data Source is only available to the application itself, thus it cannot be shared among other ones. In addition, this makes the configuration depend on the application.
  • Inside {tomcat installation directory}/conf directory: this is the preferred place because the JNDI Data Source will be available to all web applications and it’s independent of any applications.

The Below table explains the attributes specified in the above configuration:

Attribute name Description
name This represents the name of the resource.
auth It specifies the authentication mechanism for the application code, the value can be Application or Container.
type The fully qualified Java class name is expected by the web application when it performs a lookup for this specified resource.
validationQuery This parameter basically used to validate connections from the connection pool before returning them to the caller.
validationInterval It specifies to avoid excess validation, only run validation at most at this frequency – time in milliseconds.
maxActive It specifies the maximum number of database connections in the pool. We can set it to -1 for no limit imposed.
minIdle It Specifies the minimum number of established connections that should be kept in the pool at all times.
maxWait It specifies the maximum time to wait for a database connection to become available in ms(milliseconds), in this example 10 seconds. An Exception will be thrown if this timeout is crossed. You can set it to -1 to wait indefinitely.
initialSize This attribute is used to set the initial number of connections that are created when the connection pool is started. The default value is 10
jmxEnabled It specifies to register the pool with JMX or not. The default value is true.
username It specifies MySQL database user name.
password It specifies MySQL database user password.
driverClassName Here we specify the fully qualified class name of the database driver. For MySQL Connector/J, it is com.mysql.jdbc.Driver.
url It specifies the JDBC connection URL.

For more information about the attributes, visit the reference link: Here

NOTES:

  • If you are using Tomcat inside Eclipse IDE then you will have to modify the XML file under the Servers project.
  • That is because Eclipse IDE made a copy of the Tomcat configuration.
  • If two resources with the same name are both declared in the XML files under the web application’s META-INF directory and in the {tomcat installation directory}/conf directory, then the internal version(META-INF\context.xml) takes precedence.

3.  Below are the Java resources used like service, DAO, and Controller classes

Service Interface & Implementation class:

 

DAO Interface & Implementation class:

 

Model class:

 

Utility class that provides database connection using DataSource:

 

Controller Class:

4. Configure web.xml

Add the following declaration into the web.xml file:

This is necessary in order to make the JNDI Data Source available to the application under the specified namespace jdbc/bookstoreDB

5. Code a test JSP page

Now, create a JSP pages (bookList.jsp & error.jsp) to  test the application.

bookList.jsp

 

error.jsp  for error handling:

Here is the sample output when you run this application:

 

Download Source Code From Github

You May Alos Like:

What is Apache Tomcat?
What is SSL (Secure Sockets Layer)?
How to Change the Default Port of the Tomcat Server?

That’s All about Configuring JNDI Data Source for Database Connection Pooling in Tomcat?

Leave a Reply

Your email address will not be published.