You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Matt Barre <mb...@digiterra.com> on 2001/08/03 22:14:00 UTC

tomcat 4 datasources

I know there is a way to configure DataSources using tomcat 4's server.xml file. I've
tried using the default examples to create my own, but so far I haven't had any luck. The
docs for this portion of the project appear to not have been written yet. Can anyone point
me toward a resource where I can learn more about them?

Matt


Re: tomcat 4 datasources

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 3 Aug 2001, Matt Barre wrote:

> I know there is a way to configure DataSources using tomcat 4's
> server.xml file. I've tried using the default examples to create my
> own, but so far I haven't had any luck. The docs for this portion of
> the project appear to not have been written yet. Can anyone point me
> toward a resource where I can learn more about them?
> 
> Matt
> 
> 

The example in the "conf/server.xml" file is almost but not quite
right.  It should look like this:

<Context path="/examples" ...>

  ...

  <Resource name="jdbc/EmployeeAppDb" auth="SERVLET"
            type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/EmployeeAppDb">
    <parameter>
      <name>user</name>
      <value>.....</value>          <!-- Your database username -->
    </parameter>
    <parameter>
      <name>password</name>
      <value>.....</value>          <!-- Your database password -->
    </parameter>
    <parameter>
      <name>driverClassName</name>
      <value>org.hsql.jdbcDriver</value>
    </parameter>
    <parameter>
      <name>driverName</name>
      <value>jdbc:HypersonicSQL:database</value> <!-- Database URL -->
    </parameter>
  </ResourceParams>

  ...

</Context>

This example assumes that you're using the Hypersonic database and JDBC
driver (which must be in $CATALINA_HOME/common/lib so that it is visible
to both the Catalina internal classes and your application).

>From your web application's perspective, this registration configures a
data source (i.e. connection pool) that you can reference and use on
Tomcat exactly the way you can use data sources on J2EE servers.  First,
in your web.xml file, you would include something like this:

<web-app>

  ...

  <resource-ref>
    <description>Employee Database</description>
    <res-ref-name>jdbc/EmployeeAppDb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>

  ...

</web-app>

Now, in your servlet, you can use a Connection from the pool like this:

  import java.sql.Connection;
  import javax.naming.Context;
  import javax.sql.DataSource;

  ...

  Context initialContext = new InitialContext();
  DataSource dataSource = (DataSource)
   initialContext.lookup("java:comp/env/jdbc/EmployeeAppDb");
  Connection conn = dataSource.getConnection();
  ... use the connection to access the database ...
  conn.close();  // Returns the connection to the pool

NOTE:  In order to use this capability, you also need a "data source"
implementation.  By default, Tomcat 4 expects to use Tyrex 0.9.7, which
you can download from <http://tyrex.exolab.org>.  Place the Tyrex JAR file
into the "$CATALINA_HOME/common/lib" directory, along with the JDBC
driver.

Craig McClanahan

PS:  If you try this with Tomcat 4.0 beta 6, or any nightly build prior to
20010805, you will find that Tomcat doesn't include all the necessary
classes in the binary distribution, so you'll have to build it from
source.  From nightly build 20010805 on, the required interface classes
*are* included.

PPS:  I haven't tried it yet, but Servlet 2.3 (and J2EE 1.3) introduce a
new <resource-env-ref> element for web.xml that is simpler to
configure.  It should be possible to replace the <resource-ref> element
above with:

  <resource-env-ref>
    <description>Employee Database</description>
    <resource-env-ref-name>jdbc/EmployeeAppDb</resource-env-ref-name>
    <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
  </resource-env-ref>