Setting the JNDI Database Connection puddle inward Spring in addition to Tomcat is pretty easy. Tomcat server documentation gives plenty information on how to setup connectedness puddle inward Tomcat 5, six or 7. Here nosotros volition utilisation Tomcat vii along amongst saltation framework for creating a connectedness puddle inward Tomcat server in addition to accessing them inward Spring using JNDI code. In our final article, nosotros accept seen how to setup database connectedness puddle inward Spring for centre Java application which doesn't run on a spider web server or application server in addition to doesn't accept managed J2EE container. but if you lot are developing a spider web application than its improve to utilisation server managed connectedness pool in addition to access them using JNDI. Spring configuration volition hold upwards generic in addition to exactly based on JNDI refer of Datasource thus it volition run on whatsoever J2EE Server e.g. Glassfish, WebLogic, JBoss or WebSphere until JNDI refer is same.
Btw, Tomcat is my favorite web server in addition to I utilisation it a lot on evolution equally it comes integrated amongst IDE similar Eclipse in addition to Netbeans. I am using it for all exam in addition to evolution purpose, in addition to many companies fifty-fifty run Tomcat inward Production for hosting Java spider web application.
It's simple, fast in addition to real robust, though beware amongst java.lang.OutOfMemoryError: PermGen infinite inward tomcat, which tin own a retentiveness leak inward Java application. It unremarkably happens due to ThreadLocal variables in addition to JDBC drivers but you lot tin certainly avoid that past times knowing more
Btw, Tomcat is my favorite web server in addition to I utilisation it a lot on evolution equally it comes integrated amongst IDE similar Eclipse in addition to Netbeans. I am using it for all exam in addition to evolution purpose, in addition to many companies fifty-fifty run Tomcat inward Production for hosting Java spider web application.
It's simple, fast in addition to real robust, though beware amongst java.lang.OutOfMemoryError: PermGen infinite inward tomcat, which tin own a retentiveness leak inward Java application. It unremarkably happens due to ThreadLocal variables in addition to JDBC drivers but you lot tin certainly avoid that past times knowing more
How to utilisation JNDI database connectedness puddle inward Tomcat in addition to Spring
There 3 steps to configure in addition to run JNDI Datasource Connection pool for any Java Web application:
1) Configure information source inward Server in addition to create JNDI name.
2) Configure web.xml
3) Configure Spring edible bean amongst JNDI Datasource
4) Include JDBC driver library on Server lib e.g. tomcat/lib
In guild to create JNDI DataSource on J2EE spider web server you lot take to follow server documentation. On Tomcat six you lot tin but position next slice of XML in context.xml to create Tomcat managed database connectedness 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>
<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>
Tomcat internally utilisation DBCP in addition to Commons puddle library for managing database connectedness pool. You tin banking concern gibe tomcat/lib directory for scandalise file tomcat-dbcp.jar which is responsible for creating database connectedness puddle within tomcat server.
1. web.xml configuration to access JNDI Database connectedness pool
In guild to access whatsoever server resources from your spider web application, you lot take to specify the JNDI resources inward web.xml.
You can utilisation the next XML to declare JNDI Datasource inward web.xml:
You can utilisation the next XML to declare JNDI Datasource inward 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>
<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 spider web application volition run into JNDI Datasource created inward tomcat amongst refer jdbc/springDataSource.
Btw, if you lot are non familiar amongst JNDI in addition to other Java EE concepts thus I starting fourth dimension propose you lot become through JDBC driver scandalise file. I unremarkably position JAR file within lib directory of tomcat but you lot tin position it anywhere it makes feel in addition to modifies tomcat classpath to include driver JAR into classpath.
Now the residue of code which uses this information source should stay the same. You tin acquire Spring DAO source from the previous article How to setup Database Connection puddle inward Spring framework.
Btw, if you lot are non familiar amongst JNDI in addition to other Java EE concepts thus I starting fourth dimension propose you lot become through JDBC driver scandalise file. I unremarkably position JAR file within lib directory of tomcat but you lot tin position it anywhere it makes feel in addition to modifies tomcat classpath to include driver JAR into classpath.
Now the residue of code which uses this information source should stay the same. You tin acquire Spring DAO source from the previous article How to setup Database Connection puddle inward Spring framework.
Other Spring Framework Tutorials in addition to Resources for farther learning
Spring Framework 5: Beginner to Guru
Spring in addition to Hibernate Guide for Beginners
Spring Framework 5: Beginner to Guru
Spring in addition to Hibernate Guide for Beginners
P.S. - If you lot desire to acquire how to prepare RESTful Web Service using Spring MVC inward depth, I propose you lot bring together the REST amongst Spring certification class past times Eugen Paraschiv. One of the best course of didactics to acquire REST amongst Spring MVC.