Sabtu, 21 Juni 2014

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




Setting 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 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 web server or application server and doesn't have managed J2EE
container. but if you are developing 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. Tomcat is my favorite web
server
and I use it a lot on development and its comes integrated with IDE like Eclipse
and Netbeans. I am using it for all test and development purpose, Though beware with
java.lang.OutOfMemoryError: PermGen space in tomcat,







How to use JNDI database connection pool in
Tomcat and Spring



JNDI Database connection pool in Tomcat and access 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





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:






version="1.0" encoding="UTF-8"?>


antiJARLocking="true" path="/springDataSourceDemo">

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










Resource element will create JNDI datasource which can be referenced
using JNDI name
"jdbc/springeDataSource".
Tomcat internally use DBCP and Commons pool library for managing database
connection pool. you can check tomcat/lib directory  for jar
file
tomcat-dbcp.jar which is responsible for creating
database connection pool inside tomcat server.





web.xml
configuration to access JNDI Database connection pool


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


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






 

        Oracle
Spring JNDI Datasource

        jdbc/springDataSource

        javax.sql.DataSource

        Container







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





Spring
configuration for accessing JNDI Datasource :


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






id="springDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

  name="jndiName" value="java:comp/env/jdbc/springDataSource"/>

  name="lookupOnStartup" value="true"/>

  name="proxyInterface" value="javax.sql.DataSource"/>







Now final step is to make sure tomcat lib has JDBC
driver jar file
. I usually put JAR file inside lib directory of tomcat but
you can put it anywhere it make sense and modify tomcat classpath to include
driver JAR into classpath.
Now rest of code which uses this datasource should remain same. You can get
Spring DAO source from previous article How
to setup Database Connection pool in Spring framework
.








Other Spring Framework Tutorials from Javarevisited blog





























Source:http://javarevisited.blogspot.com/2013/01/how-to-setup-jndi-database-connection-pool-tomcat-spring-example-tutorial.html

Tidak ada komentar:

Posting Komentar