DBCP Configuration with Tomcat 4.1.27 and mySQL

From: Tom Martin (webmaster_at_sanjamar.com)
Date: 12/29/03

  • Next message: Majorinc, Kazimir: "Re: My webpgage"
    Date: 29 Dec 2003 12:24:17 -0800
    
    

    I'm a Java Web developer NEWBIE that has inherited a website that
    fails every 2 hours due to poor connection pooling between Tomcat
    4.0.6 and mySQL. In efforts to resolve this problem, I've created
    another replica DEVELOPMENT website and upgraded it to Tomcat 4.1.27.
    I was told this version of Tomcat supports Database Connection Pooling
    (DBCP) better than previous versions.

    I followed the instructions as listed at:
    <http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html>
    with minor configurations to adjust for my environment.

    Something I've done doesn't seem to be working... Could someone
    please assist?

    Inserted below, the error message and snippets of the configuration
    files; ERROR on WEB PAGE, Server.xml, web.xml, Prop.java, CatMan.java,
    and showproducts.jsp.

    ==========ERROR on WEB PAGE==================

    type Exception report

    message

    description The server encountered an internal error () that prevented
    it from fulfilling this request.

    exception

    org.apache.jasper.JasperException
            at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
            at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
            at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain...............
    root cause

    java.lang.NullPointerException
            at jsp.sanjamar.test.CatMan.SelectSubCategories(CatMan.java:110)
            at jsp.sanjamar.test.CatMan.ListSubCategories(CatMan.java:54)
            at org.apache.jsp.showproducts_jsp._jspService(showproducts_jsp.java:135)
            at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
            at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)................

    ==========SERVER.XML=========================
          <Host name="www.sanjamar2003.kattare.com" debug="0"
    appBase="public_html" unpackWARs="true" autoDeploy="true">
            <Alias>sanjamar2003.kattare.com</Alias>
            <Alias>maya.kattare.com</Alias>

            <Logger className="org.apache.catalina.logger.FileLogger"
                     directory="logs"
    prefix="sanjamar2003.kattare.com_log." suffix=".txt"
                    timestamp="true"/>
    <Context path="/sanjamar" docBase="sanjamar"
                                    debug="5" reloadable="true" crossContext="true">
                    
                      <Logger className="org.apache.catalina.logger.FileLogger"
                                             prefix="localhost_DBCP_log." suffix=".txt"
                                             timestamp="true"/>
                    
                      <Resource name="jdbc/sanjamar"
                                               auth="Container"
                                               type="javax.sql.DataSource"/>
                    
                      <ResourceParams name="jdbc/sanjamar">
                            <parameter>
                              <name>factory</name>
                              <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
                            </parameter>
                            <parameter>
                              <name>maxActive</name>
                              <value>100</value>
                            </parameter>
                    
                            <parameter>
                              <name>maxIdle</name>
                              <value>30</value>
                            </parameter>
                    
                            <parameter>
                              <name>maxWait</name>
                              <value>10000</value>
                            </parameter>
                            
                            <parameter>
                  <name>removeAbandoned</name>
                  <value>true</value>
                </parameter>
                            
                            <parameter>
                  <name>removeAbandonedTimeout</name>
                  <value>60</value>
                </parameter>
                            
                            <parameter>
                             <name>username</name>
                             <value>xxxxxxxxxxxxxxx</value>
                            </parameter>
                            
                            <parameter>
                             <name>password</name>
                             <value>xxxxxxxxxxxxxxxxx</value>
                            </parameter>
            
                            <parameter>
                               <name>driverClassName</name>
                               <value>org.gjt.mm.mysql.Driver</value>
                            </parameter>
                    
                            <parameter>
                              <name>url</name>
                              <value>jdbc:mysql://dc1-mysql-01.kattare.com:3306/sanjamar</value>
                            </parameter>
                            
                      </ResourceParams>
                    </Context>
          </Host>

    ==========WEB.XML============================
    <web-app>
      <description>MySQL App</description>
      <resource-ref>
          <description>DB Connection</description>
          <res-ref-name>jdbc/sanjamar</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
      </resource-ref>
    </web-app>

    ==========Prop.java============================

    public Prop() {
            // Setting up database connection, etc.
       
            errout = new PrintWriter(System.err);
            
            try {
                Class.forName("org.gjt.mm.mysql.Driver");
            } catch (ClassNotFoundException e) {
                errout.println("ClassNotFoundException: "
                +e.getMessage());
                System.err.println("ClassNotFoundException: "
                +e.getMessage());
            } catch (Exception e2) {
                System.err.println("Exception: "+e2.getMessage());
            }
            try {
                  
                  Context ctx = new InitialContext();
                  if(ctx == null )
                      throw new Exception("No Context");
                  DataSource ds =
    (DataSource)ctx.lookup("java:comp/env/jdbc/sanjamar");
                  if (ds != null) {
                    Connection con = ds.getConnection();
                    /*Original Code
                    con =
    DriverManager.getConnection("jdbc:mysql://127.0.0.1/sanjamar",
    "xxxxxxxxxxxx", "xxxxxxxxxxx"); */
                    stmt = con.createStatement();
                  }
                
            } catch (SQLException ex) {
                errout.println("SQLException: "+ex.getMessage());
                errout.flush();
                System.err.println("SQLException: "+ex.getMessage());
            } catch (Exception e2) {
                System.err.println("Exception: "+e2.getMessage());
            }
        } // end constructor
    ==========CatMan.java==========================
    public class CatMan extends Prop { // category manager

          public String ListSubCategories(String category) {
                    // returns the specified subcategories for the provided category
              try {
                    //Get recordset
                    SelectSubCategories(category);
                    
                    String errormsgs = new String ("Error no
    subcategories");
                    if (rs==null) return errormsgs;
                 
                    String subcategory = "";
                    
                    while (rs.next()) {
                            
                            String newsubcategory = rs.getString("name");
                            subcategory = subcategory + "<br><a href='#" + newsubcategory +
    "'>" + newsubcategory +"</a>";
                    }
                    return subcategory;
                    
              } catch (SQLException ex) {
                    errout.println("Can't get "+category+" subcategory: "+
                            ex.getMessage());
                    errout.flush();
                    System.err.println("Can't get "+category+" subcategory: "+
                            ex.getMessage());
                    return null;
              }
            }

            public boolean SelectSubCategories(String MajorCat) {
               try {
                            rs = stmt.executeQuery("SELECT name FROM prodtypes "+
                                    "WHERE category = '"+MajorCat+"' group by name order by name;");
               }
               catch (SQLException ex) {
                    errout.println("Can't List Sub Categories: "+ex.getMessage());
                    errout.flush();
                    System.err.println("Can't List Sub Categories: "+ex.getMessage());
                    return false;
               }
               return true;
            }
    }

    ==========showproducts.jsp==========================
    <%@ page import="java.net.*" %>
    <%@ page import="jsp.sanjamar.test.*" %>
    <%
            int prodid = 0; // for mouseovers
            CatMan cm = new CatMan();
            cm.setRequest(request);
            PriceDB pdb = new PriceDB();
            pdb.setRequest(request);
            if (!cm.ListTypes(category)) {
                    %><h2>Unable to list types for category
                            <%= category %></h2>
                    
          <%
                    out.flush();
                    return;
            }
            while (cm.NextType()) {
                    prodid++;
                    String type = cm.GetNext("name");
                    String subtitle = cm.GetNext("subtitle",false);

                    int numprods = 0;
                    pdb.ListProducts(subtitle);
                    while (pdb.NextProduct()) {
                            %><a name="<%= pdb.ListCol("prodnum",false) %>">
          <%
                            numprods++;
                    }
    %>


  • Next message: Majorinc, Kazimir: "Re: My webpgage"

    Relevant Pages

    • Re: DBCP Configuration with Tomcat 4.1.27 and mySQL
      ... > another replica DEVELOPMENT website and upgraded it to Tomcat 4.1.27. ... tomcat is telling you where the program fails: ...
      (comp.lang.java)
    • Re: which s/w tools should i use?
      ... I use Netbeans for Java website programming. ... generally HTML tags in JSP), can directly go from JSP to generated servlet, integrated out of the box with Tomcat, auto generates configuration files, deploys, integrated with CVS, auto starts Tomcat and allows breakpoints in JSP or servlets. ... I never was able to get all this in Eclipse, through I tried little bit with Eclipse Tomcat plugins. ...
      (comp.lang.java.softwaretools)
    • Re: When will IBM practice what they preach?
      ... Connection pooling is very important for good speeds with java apps. ... The good thing is that you chose Tomcat. ... I runs like hell - the iSeries is a wicked database server - and tomcat on ... then I know they are still trying to sell sell sell... ...
      (comp.sys.ibm.as400.misc)
    • Re: Virtual hosting with tomcat
      ... > one website will serve static web pages while other will use Java and ... > I wish static webpages website will server through port 80 while other ... > How can i do the Virtual hosting with Tomcat 5.5.7. ... Apache is THE http server and you prefer a JSP/Servlet engine to serve ...
      (comp.lang.java.programmer)
    • Re: Virtual hosting with tomcat
      ... > one website will serve static web pages while other will use Java and ... > I wish static webpages website will server through port 80 while other ... > How can i do the Virtual hosting with Tomcat 5.5.7. ... Apache is THE http server and you prefer a JSP/Servlet engine to serve ...
      (comp.lang.java.help)