I wanted to use tyrex to pool JNDI-acquired JDBC connections.  I have
attached a file called "diary" that explains how I ultimately got this
working.  Unfortunately, this required several undesirable things.  First,
I had to specify a tomcat-specific class (tyrex.jdbc.ServerDataSource)
in my web.xml.  Second, I had to patch the source to ResourceFactory.java
(patch attached in ResourceFactory-pool-patch).  Third, I had to muck
around with security polices, even though I wasn't running under a
security manager (this is a tyrex issue, not a tomcat one, so I'm not
going to go any farther with this).

The fundamental problem is that as far as I can tell, there is no way
to get tyrex to create a ServerDataSource without doing what I did.
The patch tells ResourceFactory to create a ServerDataSource (which
does pooling) instead of an EnabledDataSource (which does not) if the
web.xml-specified class is tyrex.jdbc.ServerDataSource.  It needs to
explicitly specified this way because of the logic around line 214
of TyrexDataSourceFactory.java.

So, I think the point of this mail is a feature request for a way to
specify that I want a ServerDataSource instead of an EnabledDataSource
when I ask tomcat to give me a javax.sql.DataSource resource.
The attached patch is suboptimal because it requires tomcat-specific
information in web.xml.

I do not know the tomcat source that well...my apologies if I am missing
something important.

brad
There is a postgresql server listening on its normal port on localhost.

Untar tomcat 4.0.2 binary and source distributions into /tmp.

Delete the contents of jakarta-tomcat-4.0.2/webapps.  Create webapps/ROOT
and webapps/ROOT/WEB-INF directories.  Install the attached test.jsp
and web.xml files in those directories, respectively.

Unjar jakarta-tomcat-4.0.2/lib/naming-factory.jar into /tmp.  Copy
jakarta-tomcat-4.0.2-src/catalina/src/share/org/apache/naming/factory/ResourceFactory.java
into /tmp/org/apache/naming/factory/.  Apply the attached
ResourceFactory-pool-patch.  Add naming-common.jar to your
classpath and build ResourceFactory.java with jikes.  Run "jar cf
jakarta-tomcat-4.0.2/lib/naming-factory.jar org".

su and add the following to /usr/lib/j2re1.3/lib/security/java.policy:

  grant {
    permission java.security.AllPermission;
  };

Delete the demo contexts and add the following to the appropriate place
in jakarta-tomcat-4.0.2/conf/server.xml:

  <Context path="" docBase="ROOT" debug="0">
    <Resource name="jdbc/testref" auth="Container" type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/testref">
      <parameter>
        <name>user</name>
        <value>XXXX</value>
      </parameter>
      <parameter>
        <name>password</name>
        <value>XXXX</value>
      </parameter>
      <parameter>
        <name>driverClassName</name>
        <value>org.postgresql.Driver</value>
      </parameter>
      <parameter>
        <name>driverName</name>
        <value>jdbc:postgresql://localhost/test</value>
      </parameter>
    </ResourceParams>
  </Context>

Copy postgresql-7.2.jar and ots.jar into /tmp/jakarta-tomcat-4.0.2/common/lib.  
Run /tmp/jakarta-tomcat-4.0.2/bin/startup.sh.  I make about 1000
connections over the course of a couple seconds, and six connections to
postmaster (the PostgreSQL daemon) are made.
--- 
jakarta-tomcat-4.0.1-src/catalina/src/share/org/apache/naming/factory/ResourceFactory.java
  Mon Nov 12 21:02:48 2001
+++ org/apache/naming/factory/ResourceFactory.java      Thu Feb 28 21:21:50 2002
@@ -138,7 +138,7 @@
                     }
                 }
             } else {
-                if (ref.getClassName().equals("javax.sql.DataSource")) {
+                if (ref.getClassName().equals("javax.sql.DataSource") || 
+ref.getClassName().equals("tyrex.jdbc.ServerDataSource")){
                     String javaxSqlDataSourceFactoryClassName =
                         System.getProperty("javax.sql.DataSource.Factory",
                                            Constants.TYREX_DATASOURCE_FACTORY);
<!DOCTYPE web-app PUBLIC
          "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd";>

<web-app>

  <resource-ref>
    <res-ref-name>jdbc/testref</res-ref-name>
    <res-type>tyrex.jdbc.ServerDataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

</web-app>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="javax.naming.InitialContext" %>
<%
  response.setContentType("text/plain");

  DataSource ds =
    (DataSource)new InitialContext().lookup("java:comp/env/jdbc/testref");
  Connection conn = ds.getConnection();
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT user FROM userlist");
  while(rs.next())
    out.println(rs.getString("user"));
  conn.close();
%>

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

Reply via email to