Your Tomcat version is not known, so I'm assuming the latest version 6.0.10.

Here's what should go in:
------------------------------------------------------
...../MyProject/WEB-INF/web.xml
------------------------------------------------------
<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee";
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee web-app_2_5.xsd"
        version="2.5">
 <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>

-----------------------------------------------------------------------------------------
According to the HOW-TO document I gave earlier the JAR for the JDBC
driver must be placed in Tomcat's lib folder, so
.......\apache-tomcat-6.0.10_bak_20070410\lib\
-----------------------------------------------------------------------------------------
mysql-connector-java-5.0.5-bin.jar

-----------------------------------------------------------------------------------------
Add the connection pooling configuration to your application's Context
definition
-----------------------------------------------------------------------------------------
        <Context path="" docBase="C:/dev/projects/ConnectionPooling"
reloadable="true" debug="true">
                <Resource name="jdbc/TestDB" auth="Container" 
type="javax.sql.DataSource"
                        maxActive="100" maxIdle="30" maxWait="10000"
                        username="javauser" password="javadude"
driverClassName="com.mysql.jdbc.Driver"
                        
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>    
        </Context>


-----------------------------------------------------------------------------------------
And finally, here's the sample working code:
/WEB-INF/classes/com/connectionpooltest/ConnectionPoolTest.java
----------------------------------------------------------------------------------------
package com.connectionpooltest;

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

public class ConnectionPoolTest {

   public void testConnection() throws SQLException, NamingException{
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try{
            //conn = ... get connection from connection pool ...
            Context initContext = new InitialContext();
            Context envContext  = (Context)initContext.lookup("java:/comp/env");
            DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
            conn = ds.getConnection();
        
            //stmt = conn.createStatement("select ...");
            stmt = conn.createStatement();
        
            //rs = stmt.executeQuery();
            rs = stmt.executeQuery("SELECT 'Java Connection Pooling Test' as
a FROM dual");
        
            //... iterate through the result set ...
                while (rs.next()) {
                    String s = rs.getString("a");
                    System.out.println(" s is : " + s);
                }
        
            rs.close();
            rs = null;
            stmt.close();
            stmt = null;
            conn.close(); // Return to connection pool
            conn = null;  // Make sure we don't close it twice
        
        }catch(SQLException e){
                e.printStackTrace();    
        }finally{
            // Always make sure result sets and statements are closed,
            // and the connection is returned to the pool
            if (rs != null) {
              try { rs.close(); } catch (SQLException e) { e.printStackTrace(); 
}
              rs = null;
            }
            if (stmt != null) {
              try { stmt.close(); } catch (SQLException e) { 
e.printStackTrace(); }
              stmt = null;
            }
            if (conn != null) {
              try { conn.close(); } catch (SQLException e) { 
e.printStackTrace(); }
              conn = null;
            }           
        }
   }

}

Since Connection Pooling is handled by the Container, the above code
must be called from within the context of the Container --- so it must
be called from either a Servlet or a JSP page --- it cannot be called
from the command line alone using a main method.

-Rashmi

---------------------------------------------------------------------
To start a new topic, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to