Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2003

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Ron de Waard - 14 Oct 2003 08:37 GMT
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.
GIMME - 15 Oct 2003 02:37 GMT
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:oracle:thin:@10.251.240.59:1521:webber</value>
 </parameter>
<parameter>
 <name>url</name>
 <value>jdbc:oracle:thin:@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 ...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.