Hi there,

I am using Oracle 9i, DBCP and Tomcat 4.1.12.  I am trying to configure a
JNDI datasource that implements connection pooling.  I have been following
the instructions in the JNDI how to, specifically the section for Oracle.

My problem is that the JNDI Datasource is null when I try to get it from the
context.  I cant see any exceptions being thrown anywhere.

Can anyone tell me what I have missed?

My server.xml is as follows (this entry is in the HOST tag, after the
/examples context):

        <Context className="org.apache.catalina.core.StandardContext" 
                 cachingAllowed="true" 
                 charsetMapperClass="org.apache.catalina.util.CharsetMapper"

                 cookies="true" 
                 crossContext="true" 
                 debug="5" docBase="TestDB" 
 
mapperClass="org.apache.catalina.core.StandardContextMapper" 
                 path="/TestDB" privileged="false" 
                 reloadable="true" swallowOutput="false" 
                 useNaming="true" 
                 wrapperClass="org.apache.catalina.core.StandardWrapper">
          <Logger className="org.apache.catalina.logger.FileLogger" 
                  debug="0" directory="logs" 
                  prefix="localhost_DBTest_log." 
                  suffix=".txt" timestamp="true" verbosity="1"/>
          <Resource auth="container" name="jdbc/myoracle" scope="Shareable"
type="java.sql.DataSource"/>
          <ResourceParams name="jdbc/myoracle">
            <parameter>
              <name>factory</name>
              <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
            </parameter>
            <parameter>
              <name>url</name>
              <value>jdbc:oracle:thin:@127.0.0.1:1521:RHODES</value>
            </parameter>
            <parameter>
              <name>password</name>
              <value>tiger</value>
            </parameter>
            <parameter>
              <name>maxActive</name>
              <value>20</value>
            </parameter>
            <parameter>
              <name>maxWait</name>
              <value>10</value>
            </parameter>
            <parameter>
              <name>driverClassName</name>
              <value>oracle.jdbc.driver.OracleDriver</value>
            </parameter>
            <parameter>
              <name>user</name>
              <value>scott</value>
            </parameter>
            <parameter>
              <name>maxIdle</name>
              <value>10</value>
            </parameter>
          </ResourceParams>
        </Context>


And web.xml is as follows:

  <resource-ref>
    <description>Oracle Datasource example</description>
    <res-ref-name>jdbc/myoracle</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>



I have a class as follows:

package foo;

import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class DBTest 
{
  String foo = "Not Connected";
  int bar = -1;
    
        public void init() 
        {
                try
                {
                        Context ctx = new InitialContext();
                        if(ctx == null ) 
                        {
                                throw new Exception("Boom - No Context");
                        }
                        
                        Context envContext  =
(Context)ctx.lookup("java:/comp/env");
                        DataSource ds =
(DataSource)envContext.lookup("jdbc/myoracle");
                        
                        if (ds != null)
                        {
                                Connection conn = ds.getConnection();
                                
                                if(conn != null)
                                {
                                        foo = "Got Connection
"+conn.toString();
                                        Statement stmt =
conn.createStatement();
                                        ResultSet rst =
stmt.executeQuery("select id, foo, bar from testdata");
                                        
                                        if(rst.next())
                                        {
                                                foo = rst.getString(2);
                                                bar = rst.getInt(3);
                                        }
                                        
                                        conn.close();
                                }
                        }
                        else
                        {
                                System.out.println("DS was null!");
                        }
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
        }
        
        public String getFoo() { return foo; }
        public int getBar() { return bar;}
}



And finally, a JSP as follows:

<html>
  <head>
    <title>DB Test</title>
  </head>
  <body>

  <%
    foo.DBTest tst = new foo.DBTest();
    tst.init();
  %>

  <h2>Results</h2>
    Foo <%= tst.getFoo() %><br/>
    Bar <%= tst.getBar() %>

  </body>
</html>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to