We are using Weblogic 9.2 in our development, testing, and production environments. Its usually best to use the same application server when developing on your local machine, but it can eat up a lot of resources that your computer may not handle.
I’ve run into this problem before so I decided to use Tomcat 6.0.18 on my machine for testing locally. The only thing I really had to worry about was using JNDI lookup to retrieve a DataSource for database connections.
I’ve used several references from the Tomcat documentation, but it wasn’t all too clear to me in the beginning. This guide should walk you through step by step to configure and use JDBC DataSources on Tomcat 6 usign JNDI lookup.
I’ll be configuring a JDBC DataSource using the Oracle JDBC drivers.
Assuming you already have Tomcat installed and properly running, your directory structure should look like the following:
/bin
/conf
/lib
/logs
/temp
/webapps
/work
in the conf directory, you will see these files:
/conf catalina.policy catalina.properties context.xml logging.properties server.xml tomcat-users.xml web.xml
You will need to modify the server.xml file
Inside the <GlobalNamingResources> block, you will need to add the following entry:
<Resource name="jndi_name_goes_here" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@db_host:db_port:db_sid" username="user_id" password="pwd"/>
So, as an example, my entry looks like the following:
<Resource name="jdbc/coast" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@development_host:7000:coast" username="coast" password="coast" maxActive="20" maxIdle="10" maxWait="-1"/>
Since I am using Oracle’s JDBC drivers, I will need to add their jar file in the tomcat’s lib directory.
You can download it from here:
http://download.oracle.com/otn/utilities_drivers/jdbc/10204/ojdbc14.jar
or
http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_10201.html
Next, assuming you already have a web application created, your directory structure should have these directories and files:
web_app_directory
/WEB-INF /WEB-INF/web.xml /META-INF /META-INF/context.xml
Next, you will need to add a META-INF directory to your web project.
Inside that META-INF directory, you will need to add a context.xml file.
Your context.xml file should look like the following:
<Context> <ResourceLink name="jdbc/coast" global="jdbc/coast" type="javax.sql.DataSource" /> </Context>
Next, you will need to update the web.xml file in the WEB-INF folder.
You should add an entry in the end of the file before closing the <web-app> tag like this:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> ... ... <resource-ref> <description>CoastDatasource</description> <res-ref-name>jdbc/coast</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
You should now be able to retrieve the DataSource within your web application.
You can test your configuration by creating a small jsp with the following code:
<%@ page import="javax.naming.Context" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.sql.DataSource" %>
<%
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
%>