Posts Tagged ‘JNDI

01
Sep
09

JNDI configuration on Apache Tomcat 6.0

This page describes how to setup JDBC Resources in Tomcat 6.

Full downloadable source for this page is available here. Corrections and enhancements are welcome, fork, change and push back to GitHub.

Database Drivers

Place the database driver jar file into the server/lib folder.

Define the resource in Tomcat

Place the following into the $CATALINA_BASE/conf/context.xml

  <Resource name="jdbc/batch" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="username" password="password" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/batch"/>

Please note that if you are running in Eclipse and have Tomcat 6 setup as a WTP project then this file is located in the “Server” project in your eclipse workspace.

Create the resource reference

The following step is important since every application server has their own JNDI Namespace. Defining the resource reference ensures that you don’t tie your application to a particular proprietary implementation. (See my post on JNDI JBoss 5)

src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
	<servlet-name>TestServlet</servlet-name>
	<servlet-class>com.test.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>TestServlet</servlet-name>
	<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
 
<resource-ref>
 <description>Recipe Database</description>
 <res-ref-name>jdbc/batch</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
 </resource-ref>

</web-app>

Test using Snoop JSP

To test the datasource simply create a snoop jsp to perform a lookup.

Test using your own Servlet

src/main/java/com/test/TestServlet.java

package com.test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//You may use the following lines to initialize the context.
		// Obtain our environment naming context
		Context initCtx;
		try {
			initCtx = new InitialContext();
			Context envCtx = (Context) initCtx.lookup("java:comp/env");
			 
			//A data source can be obtained by doing the following.
			// Look up our data source
			DataSource ds = (DataSource) envCtx.lookup("jdbc/batch");
			 
			// Allocate and use a connection from the pool
			Connection conn = ds.getConnection();
			//... use this connection to access the database ...
			System.out.println("connected: " + !conn.isClosed());
			
			conn.close();		
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

If you are using the popular spring framework you can do the following


<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/batch"/>
    <property name="resourceRef" value="true"/>
 </bean>
Full downloadable source for this page is available here.



Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 75 other followers

April 2017
S M T W T F S
« Mar    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Blog Stats

  • 806,836 hits