You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Carlton Lo <cl...@plurisinc.com> on 2002/06/19 17:10:28 UTC

tomcat JNDI DB connection pool to Oracle

I've been successfully making JDBC connections directly in my JSP 

Class.forName("oracle.jdbc.driver.OracleDriver");
ora_conn = DriverManager.getConnection("jdbc:oracle:oci8:@oracle8", "user", "password");

Not I try to setup my Tomcat 4.0.2's JNDI DB connection pool, but I can't get it to work

Here is what I've done so far

1. Move the Oracle Drivers (classes12.zip) into <TOMCAT_HOME>\lib and 

<TOMCAT_HOME>\common\lib directories and rename them to classes12.jar


2. Added the following codes to my webapp's web.xml

<resource-ref>
<res-ref-name>jdbc/ora</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


3. Added the following codes within the <Context> </Context> tag of <TOMCAT_HOME>\conf\server.xml

<Resource name="jdbc/ora" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/ora">
<parameter>
<name>user</name>
<value>system</value>
</parameter>
<parameter>
<name>password</name>
<value>manager</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>driverName</name>
<value>jdbc:oracle:oci8:@oracle8</value>
</parameter>
</ResourceParams> 

4. try to make connection within my JSP

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

<%
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/ora");
Connection conn1 = ds.getConnection();
out.println("Win2000 Oracle Connected!<br>");
} 
catch (SQLException E) {
out.println("<br>unable to get connection on ora !"); 
out.println("<br>SQLException: " + E.getMessage());
out.println("<br>SQLState: " + E.getSQLState());
out.println("<br>VendorError: " + E.getErrorCode());
}
%>

Results :
unable to get connection on ora ! 
SQLException: Cannot load JDBC driver class 'null' 
SQLState: null 
VendorError: 0 

Why? I'm able to make connection call JDBC directly in my other JSP, but not when I try to get the connection via JNDI Context ...??

Thank you in advnace
ku 


Carlton Lo
Pluris
150 Speen Street
Framingham, MA 01701
Tel: 508-663-1089
mailto:clo@plurisinc.com



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: tomcat JNDI DB connection pool to Oracle

Posted by Martin Jacobson <ma...@libero.it>.
Les Hazlewood wrote:

> I'm not 100% sure on this, but I'm fairly sure that Tomcat 4.0.x is not 
> fully JNDI ready.
> My switch to Tomcat 4.1.3 solved all my JNDI lookup problems.
> Also, I am using another Jakarta project: Commmons-DBCP (Database 
> Connection Pooling) as a JNDI factory in my app to connect with Oracle 
> and PostgreSQL.  It works wonderfully.
> http://cvs.apache.org/viewcvs/jakarta-commons/dbcp/
> for more information.


As I have posted on a similar thread, I have had problems getting this 
to work, but work it now does!

I upgraded to Tomcat 4.1.3, then made sure I had the latest available 
jar of commons-dbcp installed. The only real 'gotcha' was that contrary 
to the JNDI-HOWTO docs, the param-name for the database user is 
'username', AND NOT 'user'.

Note: it was only when I installed the latest .jar that I got a useful 
error message - the one supplied with Tomcat 4.1.3 may be OK in other 
respects.

BTW, I'm using MySQL, but that wasn't anywhere near where the problems were!

Martin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: tomcat JNDI DB connection pool to Oracle

Posted by Les Hazlewood <le...@hazlewood.com>.
I'm not 100% sure on this, but I'm fairly sure that Tomcat 4.0.x is not 
fully JNDI ready. 

My switch to Tomcat 4.1.3 solved all my JNDI lookup problems. 

Also, I am using another Jakarta project: Commmons-DBCP (Database Connection 
Pooling) as a JNDI factory in my app to connect with Oracle and PostgreSQL.  
It works wonderfully. 

http://cvs.apache.org/viewcvs/jakarta-commons/dbcp/ 

for more information. 

My setup is as follows: 

 -----------------------------------------------------
Tomcat's server.xml in the host's <Context> section:
 ----------------------------------------------------- 

         <!-- Oracle access via JNDI -->	
	  <Resource name="jdbc/oracle" auth="Container"
                   type="javax.sql.DataSource"/> 

         <ResourceParams name="jdbc/oracle">
	      <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:@127.0.0.1:1521:dbname</value>
	      </parameter>
	      <parameter>
                 <name>username</name>
	          <value>dev</value>
	      </parameter>
             <parameter>
	          <name>password</name>
	          <value>dev</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> 


	  <!-- PostgreSQL access via JNDI -->	
         <Resource name="jdbc/postgres" auth="Container"
                   type="javax.sql.DataSource"/> 

         <ResourceParams name="jdbc/postgres">
	      <parameter>
	          <name>factory</name>
		  <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
	      </parameter>
             <parameter>
	          <name>driverClassName</name>
                 <value>org.postgresql.Driver</value>
	      </parameter>
             <parameter>
	          <name>url</name>
		  <value>jdbc:postgresql://127.0.0.1:5432/dbname</value>
	      </parameter>
	      <parameter>
                 <name>username</name>
	          <value>nsi</value>
	      </parameter>
             <parameter>
	          <name>password</name>
	          <value>nsigalaxy</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> 


 -----------------------------------------------------
My webapp's web.xml:
 ----------------------------------------------------- 

<resource-ref>
     <description>
         Resource reference to a factory for javax.sql.DataSource instances
         that may be used for talking to an Oracle database that is
         configured in the server.xml file.
     </description>
     <res-ref-name>jdbc/oracle</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
 </resource-ref>
 <resource-ref>
     <description>
         Resource reference to a factory for javax.sql.DataSource instances
         that may be used for talking to a PostgreSQL database that is
         configured in the server.xml file.
     </description>
     <res-ref-name>jdbc/postgres</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
 </resource-ref> 

 -------------------------------------------------------
In java code:
 -------------------------------------------------------
Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/oracle");
Connection conn = ds.getConnection(); 

...
use it.....
... 

conn.close(); 

 


Good luck!!! 

Les Hazlewood 

P.S.  Don't forget where you declare your <resource-ref> stuff in the 
web.xml file matters!!!  Use your corresponding DTD for the xml file to find 
where (in what order) in the file you need to declare stuff. 


Carlton Lo writes: 

> I've been successfully making JDBC connections directly in my JSP  
> 
> Class.forName("oracle.jdbc.driver.OracleDriver");
> ora_conn = DriverManager.getConnection("jdbc:oracle:oci8:@oracle8", "user", "password"); 
> 
> Not I try to setup my Tomcat 4.0.2's JNDI DB connection pool, but I can't get it to work 
> 
> Here is what I've done so far 
> 
> 1. Move the Oracle Drivers (classes12.zip) into <TOMCAT_HOME>\lib and  
> 
> <TOMCAT_HOME>\common\lib directories and rename them to classes12.jar 
> 
> 
> 2. Added the following codes to my webapp's web.xml 
> 
> <resource-ref>
> <res-ref-name>jdbc/ora</res-ref-name>
> <res-type>javax.sql.DataSource</res-type>
> <res-auth>Container</res-auth>
> </resource-ref> 
> 
> 
> 3. Added the following codes within the <Context> </Context> tag of <TOMCAT_HOME>\conf\server.xml 
> 
> <Resource name="jdbc/ora" auth="Container" type="javax.sql.DataSource"/>
> <ResourceParams name="jdbc/ora">
> <parameter>
> <name>user</name>
> <value>system</value>
> </parameter>
> <parameter>
> <name>password</name>
> <value>manager</value>
> </parameter>
> <parameter>
> <name>driverClassName</name>
> <value>oracle.jdbc.driver.OracleDriver</value>
> </parameter>
> <parameter>
> <name>driverName</name>
> <value>jdbc:oracle:oci8:@oracle8</value>
> </parameter>
> </ResourceParams>  
> 
> 4. try to make connection within my JSP 
> 
> <%@ page import="java.net.*" %>
> <%@ page import="java.io.*" %>
> <%@ page import="java.sql.*" %>
> <%@ page import="javax.sql.*" %>
> <%@ page import="java.util.*" %>
> <%@ page import="javax.naming.*" %> 
> 
> <%
> try {
> Context initCtx = new InitialContext();
> Context envCtx = (Context) initCtx.lookup("java:comp/env");
> DataSource ds = (DataSource) envCtx.lookup("jdbc/ora");
> Connection conn1 = ds.getConnection();
> out.println("Win2000 Oracle Connected!<br>");
> } 
> catch (SQLException E) {
> out.println("<br>unable to get connection on ora !"); 
> out.println("<br>SQLException: " + E.getMessage());
> out.println("<br>SQLState: " + E.getSQLState());
> out.println("<br>VendorError: " + E.getErrorCode());
> }
> %> 
> 
> Results :
> unable to get connection on ora ! 
> SQLException: Cannot load JDBC driver class 'null' 
> SQLState: null 
> VendorError: 0  
> 
> Why? I'm able to make connection call JDBC directly in my other JSP, but not when I try to get the connection via JNDI Context ...?? 
> 
> Thank you in advnace
> ku  
> 
> 
> Carlton Lo
> Pluris
> 150 Speen Street
> Framingham, MA 01701
> Tel: 508-663-1089
> mailto:clo@plurisinc.com 
> 
>  
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org> 
> 
 

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>