Sunday, June 22, 2025
HomeJavaHow one can setup JNDI Database Connection pool in Tomcat

How one can setup JNDI Database Connection pool in Tomcat


Setting the JNDI Database Connection pool in Spring and Tomcat is fairly straightforward. Tomcat server documentation offers sufficient data on arrange a connection pool in Tomcat 5, 6, 7, 8, or 9. Right here we’ll use Tomcat 7 together with the spring framework for making a connection pool within the Tomcat server and accessing them in Spring utilizing JNDI code. In our final article, we now have seen arrange a database connection pool in Spring for a core Java utility that does not run on an online server or utility server and would not have a managed Java EE container.

If you’re growing an online utility then it is higher to make use of server managed connection pool and entry them utilizing JNDI. Spring configuration can be generic and simply primarily based on the JNDI identify of Datasource so it would work on any J2EE Server e.g. Glassfish, WebLogic, JBoss, or WebSphere till the JNDI identify is identical. 

Btw, Tomcat is my favourite internet server and I take advantage of it so much on improvement because it comes built-in with IDE like Eclipse and Netbeans. I’m utilizing it for all check and improvement functions, and lots of firms even run Tomcat in Manufacturing for internet hosting Java internet purposes. 

It is easy, quick, and really strong, although beware with java.lang.OutOfMemoryError: PermGen area in tomcat, which might trigger a reminiscence leak in Java utility. It often occurs because of ThreadLocal variables and JDBC drivers however you’ll be able to certainly keep away from that by figuring out extra

How one can use JNDI database connection pool in Tomcat and Spring

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

1) Configure knowledge supply in Server and create JNDI identify.

2) Configure internet.xml

3) Configure Spring bean with JNDI Datasource

4) Embody JDBC driver library on Server lib e.g. tomcat/lib
With a purpose to create JNDI DataSource on the J2EE internet server, it is advisable observe server documentation. On Tomcat 6 you’ll be able to merely put the next piece of XML in context.xml to create Tomcat managed database connection pool:


<?xml model=“1.0” encoding=“UTF-8”?>
<Context antiJARLocking=“true” path=“/springDataSourceDemo”>
<Useful resource identify=“jdbc/springeDataSource”
         auth=“Container”
         kind=“javax.sql.DataSource”
         driverClassName=“oracle.jdbc.driver.OracleDriver”
         url=“jdbc:oracle:skinny:@localhost:1521:SPRING_TEST”
         username=“root”
         password=“root”
         removeAbandoned=“true”
         removeAbandonedTimeout=“90”
         logAbandoned=“true”
         maxActive=“20”
         maxIdle=“10”
         maxWait=“-1”/>
</Context>

Useful resource component will create a JNDI knowledge supply that may be referenced utilizing JNDI identify “jdbc/springeDataSource“. 

Tomcat internally makes use of the DBCP and Commons pool library for managing the database connection pool. You may verify the tomcat/lib listing for jar file tomcat-dbcp.jar which is liable for making a database connection pool contained in the tomcat server.

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


1. internet.xml configuration to entry JNDI Database connection pool

With a purpose to entry any server useful resource out of your internet utility, it is advisable specify the JNDI sources in internet.xml. 

You can use the next XML to declare JNDI Datasource in internet.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 internet utility will see JNDI Datasource created in tomcat with the identify jdbc/springDataSource

Spring JDBC Connection Pool Example



2. Spring configuration for accessing JNDI Datasource :

This spring configuration is generic sufficient which can be utilized to entry any JNDI knowledge supply 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 identify=“jndiName” worth=“java:comp/env/jdbc/springDataSource”/>
  <property identify=“lookupOnStartup” worth=“true”/>
  <property identify=“proxyInterface” worth=“javax.sql.DataSource”/>
</bean>


I’ve used XML technique to declare a spring bean right here, however, If you’re utilizing Spring 3.0 or larger than you may as well use Java Configuration and @Bean annotation to declare knowledge supply in a Spring utility. If you’re not acquainted with Java configuration then please verify Spring Documentation to study extra about it. 


Spring JNDI Database Connection pool in Tomcat Example






3. JDBC Driver File in Tomcat Lib 

Now the ultimate step is to ensure tomcat lib has a JDBC driver jar file. I often put the JAR file contained in the lib listing of tomcat however you’ll be able to put it anyplace it is sensible and modifies the tomcat classpath to incorporate driver JAR into the classpath. 


Now the remainder of the code that makes use of this knowledge supply ought to stay the identical. You will get the Spring DAO supply from the earlier article How one can setup the Database Connection pool within the Spring framework.

JNDI Database connection pool in Tomcat and access Spring

That is all about arrange the JNDI Database connection pool in Tomcat. You may observe these steps to arrange a  DB Connection pool on your utility. Bear in mind, it is best to at all times use a DB connection pool everytime you connect with the database. It is a widespread finest follow and it’ll considerably enhance the efficiency of your Java internet utility.  


Different
Spring associated articles chances are you’ll wish to discover this weblog

  • How one can allow Spring safety in Java utility? (reply)
  • Prime 7 Programs to study Microservices in Java (programs)
  • 10 Free Programs to study Spring Framework for Learners (free programs)
  • 23 Spring MVC Interview questions for two to three years skilled (checklist)
  • How Spring MVC works internally? (reply)
  • What’s the usage of DispatcherServlet in Spring MVC? (reply)
  • Does Spring certification assist in Job and Profession? (article)
  • 5 Spring and Hibernate on-line programs for Java builders (checklist)
  • 15 Spring Boot Interview Questions for Java Builders (questions)
  • How one can put together for Spring Certification? (information)
  • 3 Finest Practices Java Builders Can study from Spring (article)
  • Distinction between @Autowired and @Injection annotations in Spring? (reply)
  • @SpringBootApplication vs @EnableAutoConfiguration? (reply)
  • 5 Spring Books Skilled Java Developer Ought to Learn (books)
  • 10 Superior Spring Boot Programs for Java builders (programs)
  • Prime 5 Programs to Be taught and Grasp Spring Cloud (programs)
  • 5 Programs to Be taught Spring Safety in depth (programs)
  • Prime 5 Spring Boot Annotations Java Builders ought to know (learn)
  • 10 Free Programs to study Spring Boot in-depth (free programs
  • Prime 5 Frameworks Java Developer Ought to Know (frameworks)
  • 7 Finest Spring Programs for Learners and Skilled (programs)
  • 10 Spring MVC annotations Java developer ought to study (annotations)
  • Prime 5 Spring Cloud annotations Java programmer ought to study (cloud)
  • 5 Programs to study Spring Cloud in depth (programs)



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments