Eugene,

I followed the example at http://jakarta.apache.org/tomcat/tomcat-4.1-doc/printer/jndi-datasource-examples-howto.html#MySQL%20DBCP%20Example.

The only thing I see wrong is the following line of code:

DataSource ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/testdev")

should be "java:comp/env/jdbc/testdev" as already mentioned earlier.

Thus, I've attached my example for you to compare. Note, DBCP connection pool requires additional jars as indicated in the README.txt file. I assume you already know this, but I mention it just incase.

Hope this helps,

Steve


Eugene Lee wrote:


I'm having a really odd problem connecting to a local MySQL server.  If
I use java.sql.DriverManager.getConnection(), it works perfectly fine.
But when I try to use a connection pool:

http://www.mysql.com/articles/connection_pooling_with_connectorj.html

it errors out:

java.sql.SQLException: Cannot load JDBC driver class 'null'

I've tried moving the MySQL driver to $CATALINA_HOME/common/lib,
$JAVA_HOME/jre/lib/ext, and even my webapp's WEB-INF/lib.  The results
are still the same.

I've followed the often-recommended Tomcat 4.1 documentation to no avail.
Here's what my box is running:

FreeBSD 4.8-RELEASE-p3 Java 2 SDK 1.3.1_09
Jakarta-Tomcat 4.1.27
MySQL Connector/J 3.0.8


I've attached my server.xml, my webapp's web.xml, and a test page
test.jsp to demonstrate the error.  BTW, before it throws the exception
on the getconnection(), the page output is:

        con1 works!
        DS lookup OK!

I appreciate any suggestions. Much thanks in advance.




------------------------------------------------------------------------


<Server port="8005" shutdown="SHUTDOWN" debug="0">

  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"
            debug="0"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"
            debug="0"/>

  <!-- Global JNDI resources -->
  <GlobalNamingResources>

    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved">
    </Resource>
    <ResourceParams name="UserDatabase">
      <parameter>
        <name>factory</name>
        <value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
      </parameter>
      <parameter>
        <name>pathname</name>
        <value>conf/tomcat-users.xml</value>
      </parameter>
    </ResourceParams>

</GlobalNamingResources>

<Service name="Tomcat-Apache">

    <Connector className="org.apache.catalina.connector.warp.WarpConnector"
               port="8008" minProcessors="5" maxProcessors="75"
               enableLookups="true" appBase="webapps" acceptCount="10"
               debug="0"/>

    <Engine className="org.apache.catalina.connector.warp.WarpEngine"
            name="Apache" debug="0">

    <Logger className="org.apache.catalina.logger.FileLogger"
            prefix="catalina_log." suffix=".txt" timestamp="true"/>

    <Host name="localhost" debug="0" appBase="webapps"
          unpackWARs="true" autoDeploy="true">

      <Logger className="org.apache.catalina.logger.FileLogger"
              directory="logs"  prefix="localhost_log." suffix=".txt"
              timestamp="true"/>

      <Context path="/testdev" docBase="testdev" debug="0" reloadable="true"
               crossContext="true">
        <Logger className="org.apache.catalina.logger.FileLogger"
                prefix="testdev_log." suffix=".txt"
                timestamp="true"/>
        <Resource name="jdbc/testdev" auth="Container"
                  type="javax.sql.DataSource"/>
        <ResourceParams name="jdbc/testdev">
          <parameter>
            <name>factory</name>
            <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
          </parameter>
          <parameter>
            <name>maxActive</name>
            <value>500</value>
          </parameter>
          <parameter>
            <name>maxIdle</name>
            <value>500</value>
          </parameter>
          <parameter>
            <name>maxWait</name>
            <value>10000</value>
          </parameter>
          <parameter>
            <name>username</name>
            <value>********</value>
          </parameter>
          <parameter>
            <name>password</name>
            <value>********</value>
          </parameter>
          <parameter>
            <name>driverClassName</name>
            <value>com.mysql.jdbc.Driver</value>
          </parameter>
          <parameter>
            <name>url</name>
            <value>jdbc:mysql://localhost/testdev</value>
          </parameter>
        </ResourceParams>
      </Context>

</Host>

<Realm className="org.apache.catalina.realm.MemoryRealm" />

</Engine>

</Service>

</Server>


------------------------------------------------------------------------


<?xml version="1.0" encoding="UTF-8"?>
<!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 id="TESTDEV">
    <display-name>testdev</display-name>
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>application</param-name>
            <param-value>resource</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>validate</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <taglib>
        <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-template.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/application-1.0</taglib-uri>
        <taglib-location>/WEB-INF/lib/application.jar</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/page-1.0</taglib-uri>
        <taglib-location>/WEB-INF/lib/page.jar</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/request-1.0</taglib-uri>
        <taglib-location>/WEB-INF/lib/request.jar</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/response-1.0</taglib-uri>
        <taglib-location>/WEB-INF/lib/response.jar</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/session-1.0</taglib-uri>
        <taglib-location>/WEB-INF/lib/session.jar</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>jspsql</taglib-uri>
        <taglib-location>/WEB-INF/lib/jspsql.jar</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/i18n-1.0</taglib-uri>
        <taglib-location>/WEB-INF/lib/i18n.jar</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/datetime-1.0</taglib-uri>
        <taglib-location>/WEB-INF/lib/datetime.jar</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/string-1.0</taglib-uri>
        <taglib-location>/WEB-INF/lib/string.jar</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/utility</taglib-uri>
        <taglib-location>/WEB-INF/lib/utility.jar</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/mailer-1.0</taglib-uri>
        <taglib-location>/WEB-INF/lib/mailer.jar</taglib-location>
    </taglib>

    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/testdev</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

</web-app>


------------------------------------------------------------------------


<%@ page import="javax.sql.DataSource" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="java.sql.*" %>
<html>
    <head><title>my test</title></head>
<body>
<%

try
{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con1 = 
java.sql.DriverManager.getConnection("jdbc:mysql://localhost/testdev?user=********&password=********");
    con1.close();
    out.println("con1 works!");
}
catch(Exception e)
{
    System.out.println(e);
}

try
{
    Context ctx = new InitialContext();
    DataSource ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/testdev");
    out.println("<br>\nDS lookup OK!");
    Connection con2 = ds.getConnection();
    out.println("<br>\ncon2 getConnection OK!");
    con2.close();
}
catch(Exception e)
{
    System.out.println(e);
}
%>

</body>
</html>


------------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
commons-collections-2.1: commons-collections.jar
commons-dbcp-1.0: commons-dbcp.jar
commons-pool-1.0: commons-pool.jar
mysql-connector-java-3.0.8-stable.jar

copy all jars above into <CATALINA_HOME>/common/lib
<Context path="/jdbcrealm" docBase="C:\VirtuasEducation\Tomcat\jdbcrealm\dist\jdbcrealm"
    swallowOutput="true" reloadable="true" debug="99" >
    <Loader checkInterval="1" />
    <Logger
        className="org.apache.catalina.logger.FileLogger"
        debug="99"
        prefix="jdbcrealm_log." suffix=".txt" 
        timestamp="true" verbosity="4">
    </Logger>

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

    <ResourceParams name="jdbc/TestDB">
        <parameter>
            <name>factory</name>
            <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
        </parameter>
        <parameter>
            <name>username</name>
            <value>mysql</value>
        </parameter>
        <parameter>
            <name>password</name>
            <value>mysql</value>
        </parameter>
        <parameter>
            <name>driverClassName</name>
            <value>com.mysql.jdbc.Driver</value>
        </parameter>
        <parameter>
            <name>url</name>
            <value>
                jdbc:mysql://localhost/javatest?autoReconnect=true
            </value>
        </parameter>
        <parameter>
            <name>removeAbandoned</name>
            <value>true</value>
        </parameter>
        <parameter>
            <name>removeAbandonedTimeout</name>
            <value>60</value>
        </parameter>
        <parameter>
            <name>logAbandoned</name>
            <value>true</value>
        </parameter>
        <parameter>
            <name>maxActive</name>
            <value>100</value>
        </parameter>
        <parameter>
            <name>maxIdle</name>
            <value>10000</value>
        </parameter>
        <parameter>
            <name>maxWait</name>
            <value>10000</value>
        </parameter>
    </ResourceParams>


</Context>
package foo;

import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class DBTest {

  private String foo = "Not Connected";
  private int bar = -1;
    
  public void init() {
    try{
      Context ctx = new InitialContext();
      if(ctx == null ) 
          throw new Exception("Boom - No Context");

      DataSource ds = 
            (DataSource)ctx.lookup(
               "java:comp/env/jdbc/TestDB");

      if (ds != null) {
        Connection conn = ds.getConnection();
              
        if(conn != null)  {
            foo = "Got Connection "+conn.toString();
            Statement stmt = conn.createStatement();
            ResultSet rst = 
                stmt.executeQuery(
                  "select id, foo, bar from testdata");
            if(rst.next()) {
               foo=rst.getString(2);
               bar=rst.getInt(3);
            }
            conn.close();
        }
      }
    }catch(Exception e) {
      e.printStackTrace();
    }
 }

 public String getFoo() { 
     return foo; 
 }
 public int getBar() { 
     return bar;
 }

}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to