tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: tomcat 4 datasources
Date Sun, 05 Aug 2001 00:51:19 GMT

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"
  <ResourceParams name="jdbc/EmployeeAppDb">
      <value>.....</value>          <!-- Your database username -->
      <value>.....</value>          <!-- Your database password -->
      <value>jdbc:HypersonicSQL:database</value> <!-- Database URL -->



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:



    <description>Employee Database</description>



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)
  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 <>.  Place the Tyrex JAR file
into the "$CATALINA_HOME/common/lib" directory, along with the JDBC

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:

    <description>Employee Database</description>

View raw message