Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Tomcat 5.0.* global JNDI database connect pooling. complex config question

843841Jun 4 2004 — edited Jun 16 2004
Ok. I can't get global connection pooling to work in tomcat 5.0.24. (I am running windows xp pro)

I am using MySQL (installed on the same machine) and I have succesfully worked through the tutorial titled "MySQL DBCP Example" found at http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html

This example does not demonstrate global connection pooling, however. I want to share a single connection pool across all my web applications. I don't want a different connection pool for each webapp.

So I define the connection pool JDBC resource in the conf/server.xml file under the <GlobalNamingResources> element.

Here is the entire <GlobalNamingResources> element lifted from conf/server.xml:

(Please overlook some of the formatting mistakes due to the <code> tag interpreting the xml as java.)
<!-- Global JNDI resources -->
<GlobalNamingResources>

    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>

    <Resource name="UserDatabase"
              auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved">
    </Resource>

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

    <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>

    <ResourceParams name="jdbc/MySQL">
        <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>20000</value>
        </parameter>
        <parameter>
           <name>username</name>
           <value>webapp</value>
        </parameter>
        <parameter>
           <name>password</name>
           <value>******</value>
        </parameter>
        <parameter>
            <name>removeAbandoned</name>
            <value>true</value>
        </parameter>
        <parameter>
            <name>removeAbandonedTimeout</name>
            <value>3000</value>
        </parameter>
        <parameter>
            <name>logAbandoned</name>
            <value>true</value>
        </parameter>
        <parameter>
            <name>url</name>
            <value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
        </parameter>
    </ResourceParams>

  </GlobalNamingResources>
I am still trying to get the DBTest example (described in the link to the tomcat 5 docs above) to work, only now I want it to work using a global connection pool. So here is the contents of webapps/DBTest/WEB-INF/web.xml: (again, please overlook formatting difficulties :)
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

  <description>MySQL Test App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/MySQL</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>
The last thing I need to do, I think, is to include a <resourceLink> element in the context.xml file for this webapp. Now in tomcat 5.0.* it is not recommended that the <context> element appear in the conf/server.xml file. Instead, as I understand it, it should appear in either of the two following places (for a webapp called DBTest):
$CATALINA_HOME/conf/[engine_name]/[host_name]/DBTest.xml
$CATALINA_HOME/webapps/DBTest/META-INF/context.xml
Since I would eventually like to package each webapp in its own war file, I prefer the second option above. This will enable me to place the context.xml file within the .war file. Currently, however, I am not using .war files.

For the DBTest webapp I have the following <context> element in webapps/DBTest/META-INF/context.xml:
<context path="/DBTest" docBase="/DBTest" debug="1">
    <ResourceLink global="jdbc/MySQL" name="jdbc/MySQL" type="javax.sql.DataSource" />
</context>
Now, when I point my browser to http://localhost:8080/DBTest/test.jsp I get the following message:

javax.servlet.ServletException: Unable to get connection, DataSource invalid: "org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null', cause: No suitable driver"


For those who are interested, here is test.jsp:
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<sql:query var="rs" dataSource="jdbc/MySQL">
select id, foo, bar from javatest.testdata
</sql:query>

<html>
  <head>
    <title>DB Test</title>
  </head>
  <body>

  <h2>Results</h2>
  
<c:forEach var="row" items="${rs.rows}">
    Foo ${row.foo}<br/>
    Bar ${row.bar}<br/>
</c:forEach>

  </body>
</html>
Now I know that this is a very long and detailed question and that it is unlikely that anyone is even going to read down this far, let alone take the time to study all that XML, but if anyone is able to tell me why this setup does not allow global connection pooling, I will be pretty *@&$**% impressed. I only wish I had duke dollars to give.

Jon
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 14 2004
Added on Jun 4 2004
11 comments
269 views