pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
JNDI Datasource example
5+ messages / 2 participants
[nested] [flat]

* JNDI Datasource example
@ 2004-08-27 19:53 Mag Gam <mag0007@myfastmail.com>
  2004-08-27 23:33 ` Re: JNDI Datasource example Harry Schittler <hs@multimedia-werkstatt.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Mag Gam @ 2004-08-27 19:53 UTC (permalink / raw)
  To: pgsql-jdbc

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!



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: JNDI Datasource example
  2004-08-27 19:53 JNDI Datasource example Mag Gam <mag0007@myfastmail.com>
@ 2004-08-27 23:33 ` Harry Schittler <hs@multimedia-werkstatt.com>
  2004-08-28 13:09   ` Re: JNDI Datasource example Mag Gam <mag0007@myfastmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Harry Schittler @ 2004-08-27 23:33 UTC (permalink / raw)
  To: Mag Gam <mag0007@myfastmail.com>; +Cc: pgsql-jdbc

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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: JNDI Datasource example
  2004-08-27 19:53 JNDI Datasource example Mag Gam <mag0007@myfastmail.com>
  2004-08-27 23:33 ` Re: JNDI Datasource example Harry Schittler <hs@multimedia-werkstatt.com>
@ 2004-08-28 13:09   ` Mag Gam <mag0007@myfastmail.com>
  2004-08-28 13:44     ` Re: JNDI Datasource example Harry Schittler <hs@multimedia-werkstatt.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Mag Gam @ 2004-08-28 13:09 UTC (permalink / raw)
  To: Harry Schittler <hs@multimedia-werkstatt.com>; +Cc: pgsql-jdbc

When I run this code snipnet, I get this problem.


symbol  : variable datasource
location: class Test
        datasource =  (DataSource)envContext.lookup(jndiName);
        ^
Test.java:22: cannot resolve symbol
symbol  : variable jndiName
location: class Test
        datasource =  (DataSource)envContext.lookup(jndiName);


echo $CLASSPATH
/usr/local/j2sdk1.4.2_05/lib/tools.jar:/usr/local/j2sdk1.4.2_05/jre/lib/rt.jar:/usr/local/jakarta-tomcat-4.1.30/common/lib/servlet.jar:/usr/local/jakarta-tomcat-4.1.30/common/lib/pg74.215.jdbc3.jar


I looked everywhere, still problems with this datasource.

Thanks!

On Sat, 28 Aug 2004 01:33:51 +0200, "Harry Schittler"
<hs@multimedia-werkstatt.com> said:
> 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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: JNDI Datasource example
  2004-08-27 19:53 JNDI Datasource example Mag Gam <mag0007@myfastmail.com>
  2004-08-27 23:33 ` Re: JNDI Datasource example Harry Schittler <hs@multimedia-werkstatt.com>
  2004-08-28 13:09   ` Re: JNDI Datasource example Mag Gam <mag0007@myfastmail.com>
@ 2004-08-28 13:44     ` Harry Schittler <hs@multimedia-werkstatt.com>
  2004-08-28 14:02       ` Re: JNDI Datasource example Mag Gam <mag0007@myfastmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Harry Schittler @ 2004-08-28 13:44 UTC (permalink / raw)
  To: Mag Gam <mag0007@myfastmail.com>; +Cc: pgsql-jdbc

Mag Gam wrote:

>When I run this code snipnet, I get this problem.
>
>
>symbol  : variable datasource
>location: class Test
>        datasource =  (DataSource)envContext.lookup(jndiName);
>        ^
>Test.java:22: cannot resolve symbol
>symbol  : variable jndiName
>location: class Test
>        datasource =  (DataSource)envContext.lookup(jndiName);
>
>
>echo $CLASSPATH
>/usr/local/j2sdk1.4.2_05/lib/tools.jar:/usr/local/j2sdk1.4.2_05/jre/lib/rt.jar:/usr/local/jakarta-tomcat-4.1.30/common/lib/servlet.jar:/usr/local/jakarta-tomcat-4.1.30/common/lib/pg74.215.jdbc3.jar
>
>
>I looked everywhere, still problems with this datasource.
>  
>
Replace the variable jndiName with  "jdbc/testds", sorry I missed this one..

cu Harry



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: JNDI Datasource example
  2004-08-27 19:53 JNDI Datasource example Mag Gam <mag0007@myfastmail.com>
  2004-08-27 23:33 ` Re: JNDI Datasource example Harry Schittler <hs@multimedia-werkstatt.com>
  2004-08-28 13:09   ` Re: JNDI Datasource example Mag Gam <mag0007@myfastmail.com>
  2004-08-28 13:44     ` Re: JNDI Datasource example Harry Schittler <hs@multimedia-werkstatt.com>
@ 2004-08-28 14:02       ` Mag Gam <mag0007@myfastmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Mag Gam @ 2004-08-28 14:02 UTC (permalink / raw)
  To: Harry Schittler <hs@multimedia-werkstatt.com>; +Cc: pgsql-jdbc

Harry, 

Thanks. Looks like the code compiles fine. However, I get no response
from postgresql (logs). Do you have a full blown Java example, instead
of a small code snipet? I think I am doing something wrong with my code.

Thanks in advance.



On Sat, 28 Aug 2004 15:44:43 +0200, "Harry Schittler"
<hs@multimedia-werkstatt.com> said:
> Mag Gam wrote:
> 
> >When I run this code snipnet, I get this problem.
> >
> >
> >symbol  : variable datasource
> >location: class Test
> >        datasource =  (DataSource)envContext.lookup(jndiName);
> >        ^
> >Test.java:22: cannot resolve symbol
> >symbol  : variable jndiName
> >location: class Test
> >        datasource =  (DataSource)envContext.lookup(jndiName);
> >
> >
> >echo $CLASSPATH
> >/usr/local/j2sdk1.4.2_05/lib/tools.jar:/usr/local/j2sdk1.4.2_05/jre/lib/rt.jar:/usr/local/jakarta-tomcat-4.1.30/common/lib/servlet.jar:/usr/local/jakarta-tomcat-4.1.30/common/lib/pg74.215.jdbc3.jar
> >
> >
> >I looked everywhere, still problems with this datasource.
> >  
> >
> Replace the variable jndiName with  "jdbc/testds", sorry I missed this
> one..
> 
> cu Harry



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2004-08-28 14:02 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-08-27 19:53 JNDI Datasource example Mag Gam <mag0007@myfastmail.com>
2004-08-27 23:33 ` Harry Schittler <hs@multimedia-werkstatt.com>
2004-08-28 13:09   ` Mag Gam <mag0007@myfastmail.com>
2004-08-28 13:44     ` Harry Schittler <hs@multimedia-werkstatt.com>
2004-08-28 14:02       ` Mag Gam <mag0007@myfastmail.com>

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