Tomcat 4.1: Setting Up Connection Pooling, Adding Resource to server.xml

2004-11-26 Thread David Stevenson
The question relates to my home study of the Tomcat Server via the book
referenced below.

Added Context via Tomcat Administration page.
Added Resource, ResourceParams, and parameter tags by hand-editing
the server.xml file, and re-starting.

Reference: Ian McFarland, Peter Harrison, Mastering Tomcat Development,
Wiley Publishing, Inc., p. 283-284.

=

In Tomcat Administration Page:
Clicking on Data Sources, under Resources, under Context /tomcatbook,
under Host, gives the
following exception, perhaps indicating Tomcat 4.1 is not compatible
with this XML setup. Exception truncated for brevity's sake.

exception

org.apache.jasper.JasperException: Exception retrieving attribute
'driverClassName'
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)

=

Changes to $CATALINA/HOME/server.xml (Context tag added by Tomcat
Administration page, 
inside Context tag manually added via text editor, as recommended by
referenced book):

  Context className=org.apache.catalina.core.StandardContext
cachingAllowed=true
charsetMapperClass=org.apache.catalina.util.CharsetMapper
cookies=true crossContext=false debug=0 docBase=tomcatbook
mapperClass=org.apache.catalina.core.StandardContextMapper
path=/tomcatbook privileged=false reloadable=true
swallowOutput=false useNaming=false
wrapperClass=org.apache.catalina.core.StandardWrapper
Resource name=jdbc/tomcatbook auth=Container
type=javax.sql.DataSource 
  ResourceParams name=jdbc/tomcatbook
parameter
  nameuser/name
  valuetomcatbook/value
/parameter
parameter
  namepassword/name
 
valuePasswordIntentionallyChangedToProtectMeFromTheGuilty/value
/parameter
parameter
  namedriverClassName/name
  valuecom.mysql.jdbc.Driver/value
/parameter
parameter
  nameurl/name
  valuejdbc:mysql://localhost.localdomain/tomcatbook/value
/parameter
  /ResourceParams
/Resource
  /Context
=
Should I create a New Data Source in the Tomcat Administration Tool?
Would the following values be correct?

PropertyValue
JNDI Name:  jdbc/tomcatbook

Data Source URL:jdbc:mysql://localhost.localdomain/tomcatbook

JDBC Driver Class:  com.mysql.jdbc.Driver

User Name:  tomcat  

Password:   PasswordIntentionallyChangedToProtectMeFromTheGuilty

Max. Active Connections:4

Max. Idle Connections:  2

Max. Wait for Connection:   5000

Validation Query:   ??? select login, password from user 



=
Server Information
Tomcat Version  Apache Tomcat/4.1.24-LE-jdk14
JVM Version 1.4.2-b28
JVM Vendor  Sun Microsystems Inc.
OS Name Linux   
OS Version  2.4.20-31.9
OS Architecture i386
=

Working Java code:

import java.sql.* ;

public class TestMySQL3
{
public static void main ( String [] args )
throws Exception
{
Class.forName(com.mysql.jdbc.Driver).newInstance();

Connection connection   = null ;
Statement statement = null ;
ResultSet resultSet = null ;
String query = select login, password from user ;

try
{

connection = DriverManager.getConnection (
jdbc:mysql://localhost/tomcatbook,
tomcat, 

PasswordIntentionallyChangedToProtectMeFromTheGuilty ) ;
statement = connection.createStatement () ;
resultSet = statement.executeQuery ( query ) ;
while ( resultSet.next () )
{
String login=
resultSet.getString ( 1 ) ;
String password =
resultSet.getString ( 2 ) ;
System.out.println ( login:  + login +
 password:  + password ) ;
}
}
finally
{
if ( resultSet != null )
resultSet.close () ;
if ( statement != null )
statement.close () ;
if ( 

Re: Tomcat 4.1: Setting Up Connection Pooling, Adding Resource to server.xml,

2004-11-26 Thread David Stevenson
I missed the following important information that was in the Tomcat
documentation, that wasn't in the book I was reading. I am currently
getting the following exception in my log file when executing a servlet
that obtains a DataSource using the JNDI InitialContext. I previously
thought it might be a class not defined exception because I didn't
supply the connection pooling .jar files as required by the
instructions.

2004-11-26 20:15:24 org.apache.catalina.INVOKER.ClientListPooledServlet:
ClientListPooledServlet.init:
javax.naming.NameNotFoundException: Name java:comp is not bound in this
Context
at
org.apache.naming.NamingContext.lookup(NamingContext.java:811)
at
org.apache.naming.NamingContext.lookup(NamingContext.java:194)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at ClientListPooledServlet.init(ClientListPooledServlet.java:25)

From ClientListPooledServlet.java. Line 25 is the one with the new
InitialContext () constructor call.

public void init ( ServletConfig config )
throws ServletException
{
super.init ( config ) ;
log ( ClientListPooledServlet.init entered ) ;
try
{
// Context initCtx = new InitialContext () ;
// Context envCtx = (Context) initCtx.lookup (
java:comp/env ) ;
// ds = (DataSource) envCtx.lookup (
jdbc/tomcatbook ) ;
ds = (DataSource) new InitialContext ().lookup (
java:comp/env/jdbc/tomcatbook ) ;
}
catch ( Exception e )
{
log ( ClientListPooledServlet.init:  +
e.getMessage () ) ;
log ( ClientListPooledServlet.init: , e ) ;
throw new UnavailableException ( e.getMessage ()
) ;
}
log ( ClientListPooledServlet.init exiting ) ;
}



DBCP uses the Jakarta-Commons Database Connection Pool. It relies on
number of Jakarta-Commons componenets:

* Jakarta-Commons DBCP 1.0
* Jakarta-Commons Collections 2.0
* Jakarta-Commons Pool 1.0

These jar files along with your the jar file for your JDBC driver should
be installed in $CATALINA_HOME/common/lib.

I don't know if these versions are close enough, or not.
If not, I can go through the Jakarta Archives looking for
closer versions.

[EMAIL PROTECTED] logs]$ ll -rt $CATALINA_HOME/common/lib
...
-rw-r--r--1 davidstevenson   175426 Nov 26 20:06
commons-collections-2.1.1.jar
-rw-r--r--1 davidstevenson42492 Nov 26 20:06
commons-pool-1.2.jar
-rw-r--r--1 davidstevenson   107631 Nov 26 20:08
commons-dbcp-1.2.1.jar

Based on the Tomcat Documentation, I revamped the server.xml
configuration (I haven't modified this yet to include the
instructions under Preventing dB connection pool leaks in
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html).

  Context className=org.apache.catalina.core.StandardContext
cachingAllowed=true
charsetMapperClass=org.apache.catalina.util.CharsetMapper
cookies=true
crossContext=false debug=9 docBase=tomcatbook
mapperClass=org.apache.catalina.core.StandardContextMapper
path=/tomcatbook privileged=false reloadable=true
swallowOutput=false
useNaming=false
wrapperClass=org.apache.catalina.core.StandardWrapper
displayName=Mastering Tomcat Development
 
Logger className=org.apache.catalina.logger.FileLogger
  prefix=localhost_tomcatbook_log. suffix=.txt
  timestamp=true/
Resource name=jdbc/tomcatbook auth=Container
type=javax.sql.DataSource 
  ResourceParams name=jdbc/tomcatbook
parameter
  namefactory/name
  valueorg.apache.commons.dbcp.BasicDataSourceFactory/value
/parameter
parameter
  namemaxActive/name
  value100/value
/parameter
parameter
  namemaxIdle/name
  value30/value
/parameter
parameter
  namemaxWait/name
  value1/value
/parameter
parameter
   nameusername/name
  valuetomcat/value
/parameter
parameter
   namepassword/name
  valueRealPasswordOmitted/value
/parameter
parameter
  namedriverClassName/name
  valuecom.mysql.jdbc.Driver/value
/parameter
parameter
  nameurl/name
 
valuejdbc:mysql://localhost/tomcatbook?autoReconnect=true/value
/parameter
  /ResourceParams
/Resource
  /Context




$ more $CATALINA_HOME/webapps/tomcatbook/WEB-INF/web.xml
...
resource-ref
description
This is a reference to the data source we use
to talk to the database configured in the
server.xml file.
/description
res-ref-namejdbc/tomcatbook/res-ref-name