Monday, July 3, 2023

How to setup JNDI Database Connection pool in Tomcat - Spring Tutorial Example

Setting the JNDI Database Connection pool in Spring and Tomcat is pretty easy. Tomcat server documentation gives enough information on how to set up a connection pool in Tomcat 5, 6, 7, 8, or 9. Here we will use Tomcat 7 along with the spring framework for creating a connection pool in the Tomcat server and accessing them in Spring using JNDI code. In our last article, we have seen how to set up a database connection pool in Spring for a core Java application that doesn't run on a web server or application server and doesn't have a managed Java EE container.

If you are developing a web application then it's better to use server managed connection pool and access them using JNDI. Spring configuration will be generic and just based on the JNDI name of Datasource so it will work on any J2EE Server e.g. Glassfish, WebLogic, JBoss, or WebSphere until the JNDI name is the same. 

Btw, Tomcat is my favorite web server and I use it a lot on development as it comes integrated with IDE like Eclipse and Netbeans. I am using it for all test and development purposes, and many companies even run Tomcat in Production for hosting Java web applications. 

It's simple, fast, and very robust, though beware with java.lang.OutOfMemoryError: PermGen space in tomcat, which can cause a memory leak in Java application. It usually happens due to ThreadLocal variables and JDBC drivers but you can surely avoid that by knowing more





How to use JNDI database connection pool in Tomcat and Spring

These three steps to configure and run a JNDI Datasource Connection pool for any  Java Web application:

1) Configure data source in Server and create JNDI name.
2) Configure web.xml
3) Configure Spring bean with JNDI Datasource
4) Include JDBC driver library on Server lib e.g. tomcat/lib


In order to create JNDI DataSource on the J2EE web server, you need to follow server documentation. On Tomcat 6 you can simply put the following piece of XML in context.xml to create Tomcat managed database connection pool:



<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/springDataSourceDemo">
<Resource name="jdbc/springeDataSource"
         auth="Container"
         type="javax.sql.DataSource"
         driverClassName="oracle.jdbc.driver.OracleDriver"
         url="jdbc:oracle:thin:@localhost:1521:SPRING_TEST"
         username="root"
         password="root"
         removeAbandoned="true"
         removeAbandonedTimeout="90"
         logAbandoned="true"
         maxActive="20"
         maxIdle="10"
         maxWait="-1"/>
</Context>


Resource element will create a JNDI data source that can be referenced using JNDI name "jdbc/springeDataSource". 

Tomcat internally uses the DBCP and Commons pool library for managing the database connection pool. You can check the tomcat/lib directory for jar file tomcat-dbcp.jar which is responsible for creating a database connection pool inside the tomcat server.


How to setup JNDI Database Connection pool in Tomcat - Spring Tutorial Example



1. web.xml configuration to access JNDI Database connection pool

In order to access any server resource from your web application, you need to specify the JNDI resources in web.xml. 

You can use the following XML to declare JNDI Datasource in web.xml:

 <resource-ref>
        <description>Oracle Spring JNDI Datasource</description>
        <res-ref-name>jdbc/springDataSource</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
</resource-ref>

Now your web application will see JNDI Datasource created in tomcat with the name jdbc/springDataSource


Spring JDBC Connection Pool Example



2. Spring configuration for accessing JNDI Datasource :

This spring configuration is generic enough which can be used to access any JNDI data source deployed on any J2EE or Java EE Server. It’s not tied up with Tomcat.  The org.springframework.jndi.JndiObjectFactoryBean is used to lookup JNDI Datasource and bind with javax.sql.DataSource.

<bean id="springDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/springDataSource"/>
  <property name="lookupOnStartup" value="true"/>
  <property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>


I have used XML way to declare a spring bean here, but, If you are using Spring 3.0 or higher than you can also use Java Configuration and @Bean annotation to declare data source in a Spring application. If you are not familiar with Java configuration then please check Spring Documentation to learn more about it. 


Spring JNDI Database Connection pool in Tomcat Example




3. JDBC Driver File in Tomcat Lib 

Now the final step is to make sure tomcat lib has a JDBC driver jar file. I usually put the JAR file inside the lib directory of tomcat but you can put it anywhere it makes sense and modifies the tomcat classpath to include driver JAR into the classpath. 


Now the rest of the code that uses this data source should remain the same. You can get the Spring DAO source from the previous article How to setup the Database Connection pool in the Spring framework.

JNDI Database connection pool in Tomcat and access Spring


That's all about how to set up the JNDI Database connection pool in Tomcat. You can follow these steps to set up a  DB Connection pool for your application. Remember, you should always use a DB connection pool whenever you connect to the database. This is a common best practice and it will significantly improve the performance of your Java web application.  


Other Spring related articles you may like to explore this blog
  • How to enable Spring security in Java application? (answer)
  • Top 7 Courses to learn Microservices in Java (courses)
  • 10 Free Courses to learn Spring Framework for Beginners (free courses)
  • 23 Spring MVC Interview questions for 2 to 3 years experienced (list)
  • How Spring MVC works internally? (answer)
  • What is the use of DispatcherServlet in Spring MVC? (answer)
  • Does Spring certification help in Job and Career? (article)
  • 5 Spring and Hibernate online courses for Java developers (list)
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • How to prepare for Spring Certification? (guide)
  • 3 Best Practices Java Developers Can learn from Spring (article)
  • Difference between @Autowired and @Injection annotations in Spring? (answer)
  • @SpringBootApplication vs @EnableAutoConfiguration? (answer)
  • 5 Spring Books Experienced Java Developer Should Read (books)
  • 10 Advanced Spring Boot Courses for Java developers (courses)
  • Top 5 Courses to Learn and Master Spring Cloud (courses)
  • 5 Courses to Learn Spring Security in depth (courses)
  • Top 5 Spring Boot Annotations Java Developers should know (read)
  • 10 Free Courses to learn Spring Boot in-depth (free courses
  • Top 5 Frameworks Java Developer Should Know (frameworks)
  • 7 Best Spring Courses for Beginners and Experienced (courses)
  • 10 Spring MVC annotations Java developer should learn (annotations)
  • Top 5 Spring Cloud annotations Java programmer should learn (cloud)
  • 5 Courses to learn Spring Cloud in depth (courses)

Thanks for reading this article so far. If you have any questions or feedback then please drop a note.


1 comment :

Unknown said...

I got the idea aout database connection with pool in tomcat...its easy to understand

Post a Comment