tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Per Jensen <>
Subject JNDI DataSource HOW-TO  - PostgreSQL comments
Date Thu, 15 May 2003 08:05:28 GMT
JNDI DataSource HOW-TO  - PostgreSQL comments.

I tried to follow the JNDI DataSource HOW-TO
included with Tomcat 4.1.24. Although the example
was very helpful, it did not work for me. To make
it work, I made some changes which are documented

Software used:
PostgreSQL 7.4-dev, jdbc driver from 7.4-dev CVS
Tomcat 4.1.24
DBCP included with above Tomcat

1. Context
It was not until a <Context> tag was added for my
web application I had success. The advice to put
the <Resource> tag after the last </Context> tag
and before </Host> did not work for me.

2. Resource name 
The HOWTO sets the resource name to
"jdbc/postgres" like shown below. Perhaps it is a
good idea to rename that to "jdbc/testdb" to
signal that it's just an identifier, not a
reference to a specific db engine or driver.

<Resource name="jdbc/postgres" auth="Container"

<ResourceParams name="jdbc/postgres">

was changed to:

<Resource name="jdbc/testdb" auth="Container"

<ResourceParams name="jdbc/testdb">

I put the <resource-def> tag in web.xml in my Web
Application, e.g WEB-INF/web.xml

The important thing is that <res-ref-name> must
point to <Resource> name. In this case

  <!-- Define a DataSource Reference -->
     <description>postgreSQL Datasource example</description>

The HOWTO does *not* use a correct <res-ref-name>.

Java source 
The sourcecode below gives me a Connection
object. The select statement is not from the
example. The important thing to verify is that you
get a correct answer from the db.

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/testdb");
if (ds != null) {
    conn = ds.getConnection();
    if(conn != null)  {"Got Connection " + conn.toString());
        Statement stmt = conn.createStatement();
        ResultSet rst = 
           stmt.executeQuery("select count(*) from country");
        if( {
  "Number of rows in Country table is: '" +
rst.getInt(1) + "'");
else {
    log.error("Did not get a DataSource");

Just my 2 cents, comments welcome

Per Jensen         Linux rules! 

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message