Howto get a context/datasource spec equal to tomcat (java:comp/env)

R

Ron de Waard

Hello,

What I want to have is a single place for defining my database
specification. I'm using tomcat 4.1.24 for our web application, however I
also have some stand-alone applications using the same database.

Preferrably, I want to use the tomcat admin tool for defining the datasource
and use it in servlets/JSP but also in the stand-alone applications. I don't
want to use a full JNDI server (LDAP or whatever), because it all runs on
the same machine and I don't want extra overhead/servers.

I have looked at FSContext and managed to create and lookup a JNDI context
and datasource within. However, tomcat uses java:comp/env as a predefined
context and I can't find anywhere how to create a similar context; I got
stuck with the java: part of it. Without that I get it all working but then
there is a difference the way tomcat defines a datasource and the way I can
create it stand-alone, and I want to have it as transparant as possible.
Preferrably, I want to read the datasource from tomcat directly within a
stand-alone application, but I don't know if that is possible.
 
G

GIMME

I guess you're about as far with it as I am ...

I'm connecting to Oracle.


I've got it so that I access the JNDI datasource as

Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/RFDS");

Anyway, this is how I did it.

For starters, put the right commons jars into the server's lib
directory :

JAKARTA-NAME ZIP-FILE JAR
Commons Pool pool-1.0.1.zip commons-pool.jar
DBCP commons-dbcp-1.0.zip commons-dbcp.jar
Collections collections-2.1.zip commons-collections.jar


In server.xml before the </Hosts> tag,

put in the following for your system ... In this instance there is
an application called DBTest and this defines a JNDI datasource
named jdbc/RFDS for it.


<Context path="/DBTest" docBase="DBTest" debug="5" reloadable="true"
crossContext="true">
<Resource name="jdbc/RFDS" auth="Container"
type="javax.sql.DataSource" />
<ResourceParams name="jdbc/RFDS">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>driverName</name>
<value>jdbc:eek:racle:thin:mad:10.251.240.59:1521:webber</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:eek:racle:thin:mad:10.251.240.59:1521:webber</value>
</parameter>
<parameter>
<name>username</name>
<value>pamhar01</value>
</parameter>
<parameter>
<name>password</name>
<value>pam</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
</Context>

Create a web.xml for the DBTest application :

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<web-app>
<resource-ref>
<description>jdbc/RFDS</description>
<res-ref-name>jdbc/RFDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

Create a JSP named test.jsp . Here it is :

<%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="java.sql.*"%>

<%!

public String fetch() {
System.out.println("in fetch");
String foo = "Not Connected";
Connection conn = null;
try{
Context initCtx = new InitialContext();
if(initCtx == null )
throw new Exception("Boom - No Context");

Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/RFDS");

foo += "<br>envCtx is:" + envCtx + "<br>" ;
foo += "<br>ds is :" + ds + "<br>" ;

if (ds != null) {
conn = ds.getConnection();

foo += "in fetch : conn is " + conn + "<br>" ;

if(conn != null) {
foo = "Got Connection "+conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst =
stmt.executeQuery("select sysdate from dual");
if(rst.next()) {
foo=rst.getString(1);
}
conn.close();
}
}
else {
foo += "<br>" + conn + " null connection <br>";
}
}catch(Exception e) {
foo += "<br> exception taken connection is : " + conn + "
<br>";
e.printStackTrace();
}
return foo;
}

%>


<html>
<head>
<title>DB Test</title>
</head>
<body bgcolor=ivory >

<%= fetch() %>

</body>
</html>

Put it all together so the file structure is right ...

../DBTest
../DBTest/jsp
../DBTest/jsp/test.jsp
../DBTest/WEB-INF
../DBTest/WEB-INF/web.xml

restart tomcat and you should see todays date, as fetched from
"select sysdate from dual".

Try fetching the datasource using the JNDI description instead of the
ref-name. See if you can get that to work ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top