Hi all,

Having "felt the pain" in getting connection pooling to work with TC 4.0.3
I've knocked up a simple howto. This is for TC 4.0.3 JDK 1.4, mySQL 4.0
Alpha so YMMV.

Enjoy,

Les

I Downloaded:

Tomcat   4.0.3 LE JDK 1.4 Build
mySQL    4.0.1 alpha
mm.mysql 2.0.14

Jakarta-Commons projects
DBCP     Nightly Build 20020523
collections 2.0
pool        1.0

Install mm.mysql, DBCP, collections and pool jars into
CATALINA_HOME/common/lib

In mySQL, I created a simple DB call javatest with a single table (testdata)
and a new user (javauser) with the password javadude.


I created a new Context for my test web app, here's the full server.xml
entry:

<Context path="/DBTest" docBase="DBTest"
         debug="5" reloadable="true" crossContext="true">

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

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

      <ResourceParams name="jdbc/TestDB">
      <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>30000</value></parameter>
      <parameter><name>maxWait</name><value>100</value></parameter>
        

      <parameter><name>username</name><value>javauser</value></parameter>
      <parameter><name>password</name><value>javadude</value></parameter>
                  
     <parameter>
       <name>driverClassName</name><value>org.gjt.mm.mysql.Driver</value>
     </parameter>
            
    <parameter>
      <name>url</name><value>jdbc:mysql://localhost:3306/javatest</value>
    </parameter>
  </ResourceParams>
</Context>

And of course, you'll need this in your web.xml

<?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>  
  <description>mySQL Test App</description>
  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/TestDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
</web-app>

And a snippet of Java to use this:-

        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();
        }


That's all folks.....Hope this is of use to you.



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

Reply via email to