pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Harry Schittler <hs@multimedia-werkstatt.com>
To: Mag Gam <mag0007@myfastmail.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: JNDI Datasource example
Date: Sat, 28 Aug 2004 01:33:51 +0200
Message-ID: <412FC4DF.5020007@multimedia-werkstatt.com> (raw)
In-Reply-To: <1093636380.30796.203209982@webmail.messagingengine.com>
References: <1093636380.30796.203209982@webmail.messagingengine.com>

Mag Gam wrote:

>Does anyone have a GOOD example of PostgreSQL with Tomcat using JNDI? I
>was looking at
>http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html
>but the code they provided really did not help. If someone has some code
>please post it. Thanks in advance!
>  
>
Just case you were using the same approach as  I did and used the tomcat 
admin application to create the JNDI  resource

Lets assume a datasource named "jdbc/testds" and a context named "testctx"

put your postgres jdbc driver into $TOMCAT/common/lib, restart Tomcat

Create the datasource using the tomcat admin application, use 
org.postgresql.Driver as driverClass and 
jdbc:postgresql://localhost/<database> as URL

insert the following in your applications WEB-INF/web.xml:
  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/testds</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

Create a META-INF/context.xml and insert:
<Context path="/testctx"  reloadable="true">
    <ResourceLink name="jdbc/testds" global="jdbc/testds" 
type="javax.sql.DataSource"/>
</Context>


Use the following code to lookup the datasource:
       try{
         Context ctx = new InitialContext();
         if(ctx == null )
             throw new Exception("JNDI could not create InitalContext ");

         Context envContext  = (Context)ctx.lookup("java:/comp/env");
         datasource =  (DataSource)envContext.lookup(jndiName);

       } catch(Exception e) {
         e.printStackTrace();
       }

this works for me...

cu Harry



view thread (5+ messages)  latest in thread

Message-ID: <412FC4DF.5020007@multimedia-werkstatt.com>
Permalink:  ../412FC4DF.5020007@multimedia-werkstatt.com/
Also on:    postgresql.org/message-id/412FC4DF.5020007@multimedia-werkstatt.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: hs@multimedia-werkstatt.com, mag0007@myfastmail.com
  Subject: Re: JNDI Datasource example
  In-Reply-To: <412FC4DF.5020007@multimedia-werkstatt.com>
  References: <1093636380.30796.203209982@webmail.messagingengine.com> <412FC4DF.5020007@multimedia-werkstatt.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox