JNDI resources: JDBC connection pooling

What is JNDI resource? in simplest term the name that we will be using to identify resource(in our case connection JDBC connection pool to Oracle)

Resource creation

We will create global resource by adding its details in server.xml in tomcat.
$CATALINA_HOME/conf/server.xml

<Resource name="jdbc/myDB"
		    auth="Container"
		    type="javax.sql.DataSource"
		    username="<<DB_user>" password="<<DB_pass>>"
		    driverClassName="oracle.jdbc.OracleDriver"
		    factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
		    url="jdbc:oracle:thin:@localhost:1521:XE"
		    maxActive="20"
		    maxIdle="10"
		    maxWait="-1"
		   />

Config is simple as it looks
JNDI Name for resource will be : jdbc/myDB
username: database user name
password: database user password
driverClassName : In our case its Oracle dirvers
url: connection url to db server.

Context reference

Reference of resource will be specified in context.xml
$CATALINA_HOME/conf/context.xml

<ResourceLink name="jdbc/myDB"
           global="jdbc/myDB"
           type="javax.sql.DataSource"/>

Environment:

Tomcat 6
Oracle 10G Express edition

Working code

  1. Create user tomcat in Oracle with password tomcat
  2. Execute SQL to create table in tomcat schema
  3. Replace (or copy content of) server.xml & context.xml from config folder
  4. Copy jstl libs from lib folder
  5. Download and copy ojdbc6.jar from Oracle
  6. Copy JNDI folder in webapps

JNDI

Ref:
http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html
http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
http://www.datadisk.co.uk/html_docs/java_app/tomcat6/tomcat6_jdbc.htm