Link to home
Start Free TrialLog in
Avatar of richardsimnett
richardsimnett

asked on

Tomcat 6: Datasource Problems

Hello,
I recently converted a glassfish application over to Tomcat 6. The application functions about 95% but 5 servlets are failing with the following exception:

Cannot create JDBC driver of class '' for connect URL 'null'

The odd thing is the same exact code to interact with the databases are used in the other 95 or so servlets without any issue at all, so it is really beyond me as to why these dont work. I have been over google and the issue seems common but caused for MANY MANY MANY different reasons. I was hoping maybe someone more experienced with Tomcat than me (Im brand new to it), could have a look and tell me what Im missing or doing wrong.

I have attached a copy of the "copy" servlet to review. Here is the output generated by the servlet. It indicates that the exception is being thrown during the initialization of the datasource.

call copyList('6721','a','cl05112011104357308',61,256); SQL: During Copy - Cannot create JDBC driver of class '' for connect URL 'null'

A working fix is worth 500 points.

Thanks,
Rick

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets.administrator.lists.controls;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.*;
import java.util.*;
import java.io.*;
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
/**
 *
 * @author rick
 */
public class copy extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {


            if (request.isRequestedSessionIdValid())
            {
                //first generate a unique tablename
                java.util.Date d = new java.util.Date();
                SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyyhhmmssSSS");

                //collect the required data (list to copy, and listname)
                String list = request.getParameter("id").toString();
                String listname = request.getParameter("listname").toString();
                String userid = request.getSession().getAttribute("userid").toString();
                String parent = request.getSession().getAttribute("parent").toString();
                
                String newtablename = "cl" + sdf.format(d);

                //now setup a database connection
                StringBuilder query = new StringBuilder();

                query.append("call copyList('");
                query.append(list);
                query.append("','");
                query.append(listname);
                query.append("','");
                query.append(newtablename);
                query.append("',");
                query.append(userid);
                query.append(",");
                query.append(parent);
                query.append(");");

                out.println(query);

                InitialContext context = null;
                DataSource ds = null;
                Connection con = null;
                Statement stmt = null;
                ResultSet tmp = null;

                try
                {
                    /* Get the connection */
                    Context envContext  = (Context) new InitialContext().lookup("java:/comp/env");
                    ds = (DataSource) envContext.lookup("jdbc/data_pool");
                    con = ds.getConnection();
                    stmt = con.createStatement();

                    out.println("Execute Query");
                    /* Query and Get Result */
                    tmp = stmt.executeQuery(query.toString());

                    out.println("Done.");
                    if (tmp.next())
                    {
                    query = new StringBuilder();

                    query.append("insert into lists (listname,tablename,originalsize,currentsize,status,parent,client_id,filename) values('");
                    query.append(listname);
                    query.append("','");
                    query.append(newtablename);
                    query.append("',0,0,3,");
                    query.append(parent);
                    query.append(",");
                    query.append(userid);
                    query.append(",'')");

                    out.println(query.toString());

                    stmt.close();

                    ds = (DataSource) envContext.lookup("jdbc/tracking_pool");
                    con = ds.getConnection();
                    stmt = con.createStatement();

                    stmt.executeUpdate(query.toString());

                    out.println("List Copy Successful.");
                    }
                    else
                        out.println("Copy failed at insert list data.");
                }
                catch(SQLException sq)
                {
                    out.println("SQL: During Copy - " + sq.getMessage());
                }
                catch(Exception e)
                {
                    out.println("Exception: During Copy - " + e.getMessage());
                }
                finally
                {
                    try
                    {
                        if (con != null)
                            con.close();
                    }
                    catch(Exception e)
                    {

                    }
                }

                con = null;
                stmt = null;
                ds = null;
                context = null;
                tmp = null;
                query = null;
            }
            else
            {
                out.println("logout");
            }
        } catch (Exception e) {
            out.println(e.getMessage());
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of richardsimnett
richardsimnett

ASKER

Yes. It is the mysql connector /j 5.1.6.. I had to install it (it wasnt packaged with Tomcat along with JSTL - weird). So I stuffed it in the jre/lib/ext directory.


They are saying in similar situation it shoule be in CATALINA_HOME/lib.


Where do you have your database driver jar file located?

If you use JNDI it must be located in the container specific library directory not in your application library directory.

E.g. for Tomcat 6.0.14 it must be CATALINA_HOME/lib.

Hope that helps.
memento
for_yan,
No Dice. I removed the driver from jre/lib/ext and moved it directly to the tomcat/lib directory. It is not packaged with the application (and never was).

And these other servlets with the same code connect to the same instance of the database without any problem?
An now you moved driver - they all contniue to work and these few servlets didn't work before  and don't work now?
If so, it is difficult to understand, probably some combination of problems.
for_yan,
Yep. No problem at all. I have attached a copy of a working servlet (from the same group of "lists" servlets) that uses the same datasource.

With the moved driver all the ones that worked previously still work, the ones that were broken are still broken.



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets.administrator.lists;

import java.io.*;
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author rick
 */
public class viewlists extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {//first we check to make sure we are logged in
            if (request.isRequestedSessionIdValid())
            {
                String userid = request.getSession().getAttribute("userid").toString();

                String sort = request.getParameter("sort").toString().replaceAll(":", " ");

                StringBuilder query = new StringBuilder();

                query.append("select * from lists where (client_id = ");
                query.append(userid);
                query.append(" or client_id = 0) and active = 1");
                query.append(" order by ");
                query.append(sort);

                //out.println(query);

                InitialContext context = null;
                DataSource ds = null;
                Connection con = null;
                Statement stmt = null;
                ResultSet tmp = null;

                try
                {
                    /* Get the connection */
                    Context envContext  = (Context) new InitialContext().lookup("java:/comp/env");
                ds = (DataSource) envContext.lookup("jdbc/tracking_pool");
                    con = ds.getConnection();
                    stmt = con.createStatement();

                    /* Query and Get Result */
                    tmp = stmt.executeQuery(query.toString());

                    tmp.last();
                    int rows = tmp.getRow();
                    tmp.beforeFirst();

                    if (rows > 0)
                    {
                        while (tmp.next())
                        {
                            String lib = "";

                            int id = new Integer(tmp.getString("id")).intValue();

                            if ((id == 2987) || (id == 4869) || (id == 2985) || (id == 2986) || (id == 4870) || (id == 4871))
                            {
                                out.print("<tr class=\"listlib\">");
                                lib = "MP Library";
                            }
                            else if (tmp.getString("tablename").compareToIgnoreCase("delivered" + userid) == 0)
                            {
                                out.print("<tr class=\"autolib\">");
                                lib = "Deliverables";
                            }
                            else if (tmp.getString("tablename").compareToIgnoreCase("openers" + userid) == 0)
                            {
                                out.print("<tr class=\"autolib\">");
                                lib = "Openers";
                            }
                            else if (tmp.getString("tablename").compareToIgnoreCase("clickers" + userid) == 0)
                            {
                                out.print("<tr class=\"autolib\">");
                                lib = "Clickers";
                            }
                            else
                                out.print("<tr class=\"stat\">");
                            
                            out.print("<td class=\"ac narrow\"><input type=checkbox value=\"");
                            out.print(tmp.getString("id"));
                            out.print("\" name=\"check\"></td>");
                            out.print("<td class=\"ac cellNumber\">");

                            int status = new Integer(tmp.getString("status")).intValue();
                            int datafeed = new Integer(tmp.getString("datafeed")).intValue();

                            if (status == 3)
                            {
                                out.print("<div style=\"float: left\"><small>" + lib + "</small></div>");
                                out.print("<a href=\"manager.jsp?where=lists&where2=liststats&id=");
                                out.print(tmp.getString("id"));
                                out.print("\">");
                                out.print(tmp.getString("listname"));
                                out.print("</a>");

                                if (datafeed == 1)
                                {
                                    out.print("<small>(Feed_ID: ");
                                    out.print(tmp.getString("id"));
                                    out.print("</small>");
                                }
                            }
                            else
                                out.print(tmp.getString("listname"));
                            
                            out.print("</td>");

                            out.print("<td class=\"ac cellNumber\">");

                            if (status == 0)
                                out.print("Import Queue.");
                            else if (status == 1)
                                out.print("Scrub in Progress.");
                            else if (status == 2)
                                out.print("Import in Progress.");
                            else if (status == 3)
                                out.print("Ready For Use.");
                            else
                            {
                                out.print("<font color=red>");
                                out.print(status);
                                out.print(" - ");
                                out.print(tmp.getString("errorMsg"));
                                out.print("</font>");
                            }

                            out.println("</td></tr>");
                        }
                    }
                    else
                       out.println("<td class=\"ac cellNumber\" colspan=\"4\">Sorry, You dont have any lists.</td>");

                }
                catch(SQLException sq)
                {
                    out.println("SQL Exception During View Offers<br><br>" + sq.getMessage());
                }
                catch(Exception e)
                {
                    out.println("Exception During View Offers<br><br>" + e.getMessage());
                }
                finally
                {
                    try
                    {
                        if (con != null)
                            con.close();
                    }
                    catch(Exception e)
                    {

                    }
                }

                con = null;
                stmt = null;
                ds = null;
                context = null;
                tmp = null;
                query = null;
            }
            else
            {
                out.println("logout");
            }
        } catch (Exception e) {out.println(e.getMessage());
        } finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Open in new window

I would be tempted to remove the jar from $CATALINA_HOME/lib *as well as* the ext directory. I wouldn't be surprised if some stuff still worked
I have narrowed it down to this line that is throwing the exception:

con = ds.getConnection();

Thanks,
Rick
Can you post the full stack trace from the log?
cehj,
You're right about it still working after the removal from both places. However, the servlets are still broken. :(

>>You're right about it still working after the removal from both places.

Thought so. You need to discover which driver IS being used and where it's being loaded from. If all else fails, start the vm with verbose classloading
call copyList('6721','test','cl05112011030115511',61,256); SQL: During Copy - Cannot create JDBC driver of class '' for connect URL 'null' org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1150) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880) at servlets.administrator.lists.controls.copy.processRequest(copy.java:82) at servlets.administrator.lists.controls.copy.doGet(copy.java:171) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454) at java.lang.Thread.run(Thread.java:636) Caused by: java.sql.SQLException: No suitable driver at java.sql.DriverManager.getDriver(DriverManager.java:279) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1143) ... 17 more
The ones that ARE working might not be using that datasource at all ...
cehj,
I just checked the enter machine for *mysql*.jar, and came up with two drivers. 5.1.6 in my netbeans/ide52/lib directory and another in netbeans under ruby (5.0.4). I deleted the ruby one leaving only one on the machine.

For the record, this is being run from netbeans on the local install of tomcat while I am working the kinks out of it.

Thanks,
rick
Please post server.xml
cehj,
Actually things just started working. After removing those drivers, I shutdown tomcat and netbeans. Then restarted them and redeployed and boom, servlets work!

Thanks!
:)