tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Per Jensen <...@net-es.dk>
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
below.
 

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


server.xml
----------
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"
          type="javax.sql.DataSource"/> 

<ResourceParams name="jdbc/postgres">


was changed to:


<Resource name="jdbc/testdb" auth="Container"
          type="javax.sql.DataSource"/> 

<ResourceParams name="jdbc/testdb">


WEB-IF/web.xml
--------------
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
"jdbc/testdb"

  <!-- Define a DataSource Reference -->
  <resource-ref>
     <description>postgreSQL Datasource example</description>
     <res-ref-name>jdbc/testdb</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
  </resource-ref>


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)  {
        log.info("Got Connection " + conn.toString());
        Statement stmt = conn.createStatement();
        ResultSet rst = 
           stmt.executeQuery("select count(*) from country");
        if(rst.next()) {
            log.info("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
-- 
----------------------------------------------------------------------
Per Jensen        http://www.net-es.dk/~pj         Linux rules! 
----------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message