Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Tomcat Users »

JNDI References To Tomcat 4.1

Jason Mowat

2003-08-15


Greetings,

I am trying to create a standalone application that references a DBCP on Tomcat. I'm not sure if I can do this; I've seem some articles that seem to suggest that it can be done, but again, I'm not sure. I've read http://www.mail-archive.com/commons-dev@(protected).

I have set up in tomcat/conf/server.xml the following context info:

<DefaultContext>
  <Resource name="jdbc/test_db" auth="Container" type="javax.sql.DataSource"/>

  <ResourceParams name="jdbc/test_db">  
   <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>url</name>
    <value>jdbc:oracle:thin:@(protected)>    
   </parameter>
   
   <parameter>
    <name>username</name>
    <value>test</value>    
   </parameter>
   
   <parameter>
    <name>password</name>
    <value>test</value>    
   </parameter>
   
   <parameter>
    <name>maxActive</name>
    <value>20</value>    
   </parameter>  
   
   <parameter>
    <name>maxIdle</name>
    <value>30000</value>    
   </parameter>        
   
   <parameter>
    <name>maxWait</name>
    <value>100</value>    
   </parameter>    
  </ResourceParams>

</DefaultContext>

Note that I have not created any references in a web.xml, since I will not be running an application, per se, from Tomcat. PERHAPS THIS IS WHERE MY PROBLEM IS?

The client code I am using is not a servlet or a JSP; it's a simple piece of code to see if I can communicate with the database pool on Tomcat as configured above.

Hashtable env = new Hashtable();
env.put(
 Context.INITIAL_CONTEXT_FACTORY,
 "org.apache.commons.dbcp.BasicDataSourceFactory"
);

// *** THIS IS ANOTHER AREA WHERE I MAY BE WRONG! ***
env.put(
 Context.PROVIDER_URL,
 "http://localhost:8080"
);

try {    
 InitialContext initCtx = new InitialContext(env);
 DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/test_db");
 Connection conn = ds.getConnection();
 Statement stmt = conn.createStatement();
 ResultSet rset = stmt.executeQuery("SELECT * FROM tbl_test");
 conn.close();
 initCtx.close();      
}
catch(NamingException e) {
 fail("Naming exception thrown");
}
catch (SQLException e) {
 fail("SQL Exception thrown");
}

I start Tomcat, assuming that the pool is set up. Then I use the client to try to get an initial context from it. This throws a naming exception, because it doesn't know how to get an initial context for org.apache.commons.dbcp.BasicDataSourceFactory at http://localhost:8080.

My first question: can I do this?
My second question: If I can't do it like this, do I need to set up a JNDI provider (like on JBoss) and set up the pooling there?
My final question: It "feels" as if my problem is simply setting up the PROVIDER_URL properly so that it can get the context from Tomcat. I've done LDAP PROVIDER_URLs before, but never one that gets a data source factory from a servlet container. Can I use http over port 8080 to get the data source factory? Is there a different mechanism for getting the factory from code that is not residing within the servlet container? Can this even be done on Tomcat?

I appreciate any help that anyone can provide.

Cheers,
Jason


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@(protected)
For additional commands, e-mail: tomcat-user-help@(protected)


©2008 junlu.com - Jax Systems, LLC, U.S.A.