Anda di halaman 1dari 9

Javarevisited

Blog about Java, Programming, Spring, Hibernate, Interview Questions, Books and Online Course Recommendations from Udemy, Pluarlsight etc

Home core java spring hibernate collections multithreading design patterns interview questions coding data structure OOP java 8 books About Me

Java Certifications JDBC jsp-servlet JSON SQL Linux Courses online resources jvm-internals REST Eclipse jQuery Java IO Java XML

S U N D A Y, J U L Y 2 2 , 2 0 1 8

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 setup connection pool in Tomcat 5, 6 or 7.
Here we will use Tomcat 7 along with spring
framework for creating a connection pool in
Tomcat server and accessing them in Spring
using JNDI code. In our last article, we have
seen how to setup database connection pool in
Spring for core Java application which doesn't
run on a web server or application server and
doesn't have managed J2EE container. but if you
are developing a web application than its better to use server managed connection pool and
access them using JNDI. Spring configuration will be generic and just based on JNDI name of
Datasource so it will work on any J2EE Server e.g. Glassfish, WebLogic, JBoss or
WebSphere until JNDI name is 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 purpose, and
many companies even run Tomcat in Production for hosting Java web application.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
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
There three steps to configure and run 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 J2EE web server you need to follow server
documentation. On Tomcat 6 you can simply put following piece of XML in context.xml to
create Tomcat managed database connection pool:

<?xml version="1.0" encoding="UTF-8"?> Follow by Email

<Context antiJARLocking="true" path="/springDataSourceDemo"> Email address... Submit


<Resource name="jdbc/springeDataSource"
Interview Questions

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
auth="Container" core java interview question (168)
type="javax.sql.DataSource" Coding Interview Question (72)
driverClassName="oracle.jdbc.driver.OracleDriver" data structure and algorithm (69)
url="jdbc:oracle:thin:@localhost:1521:SPRING_TEST" interview questions (47)
username="root" object oriented programming (31)
password="root" SQL Interview Questions (30)
removeAbandoned="true" design patterns (30)

removeAbandonedTimeout="90" thread interview questions (30)

logAbandoned="true" collections interview questions (25)

maxActive="20" spring interview questions (18)

maxIdle="10" database interview questions (16)

maxWait="-1"/> servlet interview questions (15)


Programming interview question (6)
</Context>
hibernate interview questions (6)

Best of Javarevisited
Resource element will create JNDI data source which can be referenced using JNDI name
"jdbc/springeDataSource". How Spring MVC works internally?
How to design a vending machine in Java?
How HashMap works in Java?
Tomcat internally use DBCP and Commons pool library for managing database connection pool.
Why String is Immutable in Java?
You can check tomcat/lib directory for jar file tomcat-dbcp.jar which is responsible for
10 Articles Every Programmer Must Read
creating database connection pool inside tomcat server.
How to convert lambda expression to method
reference in Java 8?
10 Tips to improve Programming Skill
10 OOP design principles programmer should know
How Synchronization works in Java?
10 tips to work fast in Linux
5 Books to improve Coding Skills

Java Tutorials

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
date and time tutorial (21)
FIX protocol tutorial (15)
Java Certification OCPJP SCJP (24)
java collection tutorial (73)
java IO tutorial (28)
Java JSON tutorial (12)
Java multithreading Tutorials (55)
Java Programming Tutorials (18)
Java xml tutorial (16)
JDBC (29)
jsp-servlet (37)
online resources (105)

Followers

1. web.xml configuration to access JNDI Database Followers (4626) Next

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: Follow

Subscribe to Download the E-book


<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>

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Now your web application will see JNDI Datasource created in tomcat with name
jdbc/springDataSource.

Btw, if you are not familiar with JNDI and other Java EE concepts then I first suggest you go
through Java Web Fundamentals course by Kevin Jones on Pluarlsight. It's not free, as you
need a Plurlasight membership but you can access it free by opting for Pluralsight's 10-day free
trial.

Download Building a REST API with


The E-book Spring 4?

Email address... Submit

Categories

SQL (54)
courses (46)
linux (36)
database (31)
Eclipse (28)
Java Certification OCPJP SCJP (24)
2. Spring configuration for accessing JNDI Datasource : JVM Internals (22)
JQuery (17)
This spring configuration is generic enough which can be used to access any JNDI data source
REST (16)
deployed on any J2EE or Java EE Server. It’s not tied up with Tomcat.
Maven (11)
The org.springframework.jndi.JndiObjectFactoryBean is used to lookup JNDI
Testing (11)
Datasource and bind with javax.sql.DataSource.
general (10)

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Blog Archive
<bean id="springDataSource"
▼ 2018 (174)
class="org.springframework.jndi.JndiObjectFactoryBean">
► December (5)
<property name="jndiName"
► November (62)
value="java:comp/env/jdbc/springDataSource"/>
► October (34)
<property name="lookupOnStartup" value="true"/>
► September (17)
<property name="proxyInterface" value="javax.sql.DataSource"/>
► August (5)
</bean>
▼ July (15)
Top 5 Java Performance Tuning Books for
Experience...

I have used XML way to declare a spring bean here, but, If you are using Spring 3.0 or higher Does Oracle's Java Certifications like OCAJP,
OCEJ...
than you can also use Java Configuration and @Bean annotation to declare data source in a
How to do Pagination in Oracle Database - SQL
Spring application. If you are not familiar with Java configuration then please check Spring Quer...
Master Class - Beginner to Expert to learn more about it. Top 5 Best Java 8 Tutorials, Courses, and Books
2 Books to Prepare Oracle Java Certification
Exams...
10 Object Oriented Design Principles Java
Programm...
10 Tips To Work Fast and Improve Productivity in
B...
Spring Security Concurrent Session Control
Example...
How to setup JNDI Database Connection pool in
Tomc...
2 Ways to Setup LDAP Active Directory
Authenticati...
Top 9 Java Programming Books - Best of lot, Must
R...
Amazon Prime Day 2018 - Great Opportunity to
Buy B...
Top 5 books to learn Spring Boot and Spring Cloud
...

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
3. JDBC Driver File in Tomcat Lib Which Programming Books Would You Buy if 100$
is G...
10 Reasons to Learn Java Programming Language
Now the final step is to make sure tomcat lib has JDBC driver jar file. I usually put JAR file
and ...
inside lib directory of tomcat but you can put it anywhere it makes sense and modifies tomcat
► June (10)
classpath to include driver JAR into classpath.
► May (2)
► April (4)
► March (9)
Now the rest of code which uses this data source should remain the same. You can get Spring
► February (7)
DAO source from the previous article How to setup Database Connection pool in Spring
► January (4)
framework.
► 2017 (801)
► 2016 (131)

References

1. Oracle's Java Tech Network


2. jQuery Documentation
3. Microsoft SQL Server Documentation
4. Java SE 8 API Documentation
5. Spring Documentation
6. Oracle's JAva Certification
7. Spring Security 5 Documentation

Pages

Privacy Policy

Copyright by Javin Paul 2010-2018. Powered by


Blogger.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Other Spring Framework Tutorials and Resources for further learning
Spring Framework 5: Beginner to Guru
Spring and Hibernate Guide for Beginners
How to use Data Access Object or DAO design pattern in Java
How to measure execution time using Spring’s Stopwatch utility
How to do LDAP authentication in Java web application using Spring Security
How to control concurrent user session in Java web application – Spring Security Tutorial
A quick guide to bean scopes in Spring Framework
Spring in Action 4th edition by Craig Walls

Thanks for reading this article so far. If you like this article then please share with your friends
and colleagues. If you have any questions or feedback then please drop a note.

P.S. - If you want to learn how to develop RESTful Web Service using Spring MVC in depth, I
suggest you join the REST with Spring certification class by Eugen Paraschiv. One of the best
course to learn REST with Spring MVC.

By Javin Paul at July 22, 2018


Labels: J2EE , JDBC , spring
Location: United States

1 comment :
Abirami R said...
I got the idea aout database connection with pool in tomcat...its easy to understand

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
April 21, 2014 at 3:25 AM

Post a Comment

Enter your comment...

Comment as: Google Accoun

Publish Preview

Newer Post Home Older Post

Subscribe to: Post Comments ( Atom )

Search This Blog


Search

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD

Anda mungkin juga menyukai