?
Solved

Tomcat - Connection error from DataSource using JNDI

Posted on 2004-04-12
14
Medium Priority
?
21,616 Views
Last Modified: 2007-12-19
Hi, I'm trying to connect to an Oracle 8i database using JNDI lookup from Tomcat 4.1.30 and get an error I can't figure out.

The error message reads: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
This error seems to be when I try to get a connection from the DataSource object:
Connection conn = datasource.getConnection();

In attempts to figure this out, I created a test servlet that first connects to the database using the hard-coded driver and url, then JNDI.

The servlet doGet looks like this:
                  protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            
            PrintWriter out = response.getWriter();
            String sDriver = "oracle.jdbc.driver.OracleDriver";
            String sURL = "jdbc:oracle:thin:@somecompany.com:1521:db1";
            try {
                  out.println("Attempting Oracle connection<br>");
                  Class.forName(sDriver).newInstance();
                  Connection conn = DriverManager.getConnection(sURL, "scott", "tiger");
                  conn.close();
                  out.println("Connected to Oracle okay<br>");
            
            } catch (InstantiationException e) {
                  out.println(e.getClass()+": "+e.getMessage()+"<br>");
            } catch (IllegalAccessException e) {
                  out.println(e.getClass()+": "+e.getMessage()+"<br>");
            } catch (ClassNotFoundException e) {
                  out.println(e.getClass()+": "+e.getMessage()+"<br>");
            } catch (SQLException e) {
                  out.println(e.getClass()+": "+e.getMessage()+"<br>");
            }
            
            InitialContext ctx;
            try {
                  out.println("Attempting JNDI connection<br>");
                  ctx = new InitialContext();
                  Context envctx = (Context) ctx.lookup("java:comp/env");
                  DataSource datasource = (DataSource) envctx.lookup("jdbc/operations");
                  Connection conn = datasource.getConnection();
                  conn.close();
                  out.println("Connected to JNDI okay<br>");
            }
            catch (NamingException e) {
                  out.println(e.getClass()+": "+e.getMessage()+"<br>");
            } catch (SQLException e) {
                  out.println(e.getClass()+": "+e.getMessage()+"<br>");
            }
            
      }

The output looks like this:
Attempting Oracle connection
Connected to Oracle okay
Attempting JNDI connection
class org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

My server.xml looks like:
...
<GlobalNamingResources>
    <Environment name="simpleValue" override="true" type="java.lang.Integer" value="30"/>
     <Resource auth="Container" name="jdbc/operations" scope="Shareable" type="javax.sql.DataSource"/>
     <ResourceParams name="jdbc/operations">
        <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:@somecompany.com:1521:db1</value>
        </parameter>
        <parameter>
            <name>username</name>
            <value>scott</value>
        </parameter>
        <parameter>
            <name>password</name>
            <value>tiger</value>
        </parameter>
        <parameter>
            <name>maxActive</name>
            <value>20</value>
        </parameter>
        <parameter>
            <name>maxIdle</name>
            <value>10</value>
        </parameter>
        <parameter>
            <name>maxWait</name>
            <value>5000</value>
        </parameter>
      </ResourceParams>

  </GlobalNamingResources>
...

My web.xml looks like:
<?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/dtd/web-app_2_3.dtd">
<web-app>
      <!-- define global vars -->
      
      
      <servlet>
     <servlet-name>TestServlet</servlet-name>
     <servlet-class>testJNDI.TestJNDI</servlet-class>
    </servlet>
   
      <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/servlet/TestJNDI</url-pattern>
    </servlet-mapping>
      
      <session-config>
            <session-timeout>120<!-- in minutes --></session-timeout>
      </session-config>
      <resource-ref>
            <description>Oracle Datasource</description>
             <res-ref-name>jdbc/operations</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
</resource-ref>

      
</web-app>


Help!

Thanks in advance.
0
Comment
Question by:topher1120
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 9

Expert Comment

by:vzilka
ID: 10814080
Can you remove the factory parameter and tell us the result?
0
 
LVL 2

Author Comment

by:topher1120
ID: 10814850
Removed the parameter with the same result.  I don't know if this matters much, but its running on Win2k w/ JDK 1.4.
0
 
LVL 9

Expert Comment

by:vzilka
ID: 10815656
DId you put the classes12.jar in your web-inf/lib directory, or the TOMCAT\shared\lib directory?
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 2

Author Comment

by:topher1120
ID: 10816443
I have it in TOMCAT_HOME\common\lib.  Tried moving it to TOMCAT_HOME\shared\lib, but to no avail.
0
 
LVL 9

Expert Comment

by:vzilka
ID: 10816588
classes12.zip or classes12.jar?
0
 
LVL 2

Author Comment

by:topher1120
ID: 10818073
classes12.jar.  I even removed the javax package in it to see if it would help (saw in another post that may cause some problems.)
0
 
LVL 9

Expert Comment

by:vzilka
ID: 10830605
Double check the url property. Can it really be used to connect to the database?
YOu can write a small servlet that creates a JDBC connection and run it inside Tomcat.
This way we will know if the jdbc driver is in the classpath, and the database is valid.
0
 
LVL 2

Author Comment

by:topher1120
ID: 10837121
vzilka,  please see my initial post.  I provided the doGet method of my test servlet and the output it produces.  Although I did change the url, username and password for the post, I have checked that they are both the same.  I even tried copy-paste from the working portion to the jndi setup.  When I initially set this up, I used the Tomcat administration application.  It would really help if they had some way to check connectivity from within Tomcat :).  
0
 
LVL 2

Author Comment

by:topher1120
ID: 10837824
I believe I figured this out.  When I had added the datasource information in server.xml, I put it under "GlobalNamingResources".  Because of this and the fact I had nothing in my context that pointed to it, it couldn't find it.  As soon as I put a "ResourceLink" element in my context, the JNDI lookup succeeded.  I'm betting if I had defined the resource in the context, what I had posted would have worked.

0
 
LVL 9

Expert Comment

by:vzilka
ID: 10846719
You are right. Sorry for not picking this up immediately, this is one of the oldest in the book.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11075836
Closed, 250 points refunded.

modulo
Community Support Moderator
Experts Exchange
0
 

Expert Comment

by:gk_97
ID: 13316061
Ho topher1120

I am experincing
17:01:43,422 [ConnectDB.java] [ConnectDB] [getConnectionPoolForOracle:101] - ERR
OR - failed to get the connection: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null', cause: No suitable
driver-

I read your comment about ResourseLink.
I am unsure on where to include that.

Do i need to create a separate tag <Context> in the server.xml and include it in there?

or do i need to create a separate file context.xml and add the tag there?
I am using Tomcat 5.0.28

The resource parameters are in the <GlobalNamingResources> tag - does the context tag need to be around this?

thanks

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert websphere application server default chained Certificates from 1024 to 2048 keysize or higher size and also you can change signatureAlgorithm . Please make sure Websphere Application Server fixpack 7.0.0.23 or Above. The following steps a…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question