Re: JNDI Datasource configuration - Mailing list pgsql-jdbc

From Luke (Terry) Vanderfluit
Subject Re: JNDI Datasource configuration
Date
Msg-id 1093812821.1211.164.camel@bench.chipcity.com.au
Whole thread Raw
In response to JNDI Datasource configuration  ("Mag Gam" <mag0007@myfastmail.com>)
List pgsql-jdbc
Hi,

Do you configure the Datasource thru Tomcat's Administrator Console
> (http://localhost:8080/admin) or by editing files? I prefer doing it
> thru the Admin Console. Do you edit any other files?
>
> Currently my datasource is set like this in the console:
> JNDI Name:
> jdbc/Test
> Data Source URL: jdbc:postgresql://foo.edu:5432/Test
> JDBC Driver Class: org.postgresql.Driver
> Username: User
> Password: Password
> Max. Active Connections: 4
> Max. Idle Connections: 2
> Max. Wait for Connection: 5000
> Validation Query:
>
> Does all this look right?

Your Data Source URL should be jdbc:postgresql:Test

but I can't gather whether your setup is correct,
myself, I found some helpful notes at:

http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html
and

http://66.137.16.41:8080/expresso/expresso/ (you'll find quite a bit of
info relating to their expresso stuff but also generally interesting
information for installing jdbc

particularly this section
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To use a Tomcat data source you will have to

    *

      Define a JNDI resource reference in your Web application
deployment descriptor
    *

      Map the JNDI resource reference onto a real resource (database
connection) in the context of your application
    *

      Look up the JNDI data source resource reference in your code to
obtain a pooled database connection

Defining a Resource Reference

First, you will need a JNDI name for your database connection;
conventionally it should begin with jdbc/. The example uses the name
jdbc/conversion. Now add a <resource-ref> element to the web.xml file
for your Web application to define the data source as follows:

<resource-ref>
 <res-ref-name>jdbc/conversion</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>

The <res-ref-name> element identifies the resource reference name, the
<res-type> entry defines the reference as a JDBC data source, and the
<res-auth> specifies that resource authentication is applied by the
Container. The <resource-ref> element must be added after any
<servlet-mapping> elements in the deployment descriptor.

Defining the Resource
Second, you must add an entry for the database resource to the
<CATALINA_HOME>/conf/ser
ver.xml file by adding a <ResourceParams> element to define the database
connection inf
ormation. The <ResourceParams> can be defined inside the
<DefaultContext> tag to be ava
ilable to all Web applications, or it can be defined inside the
<Context> element for a
 specific application.

A suitable Tomcat 4.1 <ResourceParams> element for the example database
is shown in the
 following listing (Tomcat 4.0 uses different attribute names as
explained in the notes
):

<Context path="/database" docBase="database" debug="0"
     reloadable="true" >
 <ResourceParams name="jdbc/conversion">
  <parameter>
   <name>username</name>
   <value>root</value>
  </parameter>
  <parameter>
   <name>password</name>
   <value>secret</value>
  </parameter>
  <parameter>
   <name>driverClassName</name>
   <value>org.gjt.mm.mysql.Driver</value>
  </parameter>
  <parameter>
   <name>url</name>
   <value>jdbc:mysql://localhost/test</value>
  </parameter>
 </ResourceParams>
</Context>

In the <ResourceParams> element example, the name attribute must exactly
match the <res
-ref-name> value you defined in the <resource-ref> element of the
web.xml file.

    Note - As an alternative to using resource references in an
application's web.xml file, you can define the resource using a
<Resource> element in the server.xml file. You can add a <resource>
element to the <Context> element for an individual application or to the
<DefaultContext> element to define the resource for all Web
applications. The <Resource> definition for the example JDBC data source
is

        <Resource name="jdbc/conversion"

        auth="Container"

        type="javax.sql.DataSource"/>

The various <ResourceParams> components define the database connection
parameters as follows:

Parameter


Value

username


Username for the database (root); under Tomcat 4.0 this attribute is
called user

password


Password for the user root (secret)

driverClassName


Driver class for the database connection (org.gjt.mm.mysql.Driver)

url

JDBC connection string for the database (jdbc:mysql://localhost/test);
under Tomcat 4.0
 this attribute is called driverName

    Caution - The server.xml file contains the unencrypted password for
accessing the d
atabase. This file should be secured so that unauthorized users cannot
read it.

The <ResourceParams> element in the server.xml file encapsulates all the
information re
quired to access the database. When moving your application from a
development environm
ent to a live environment, you need only change the values for the
parameters in the se
rver.xml file. Neither the Java code nor the web.xml resource reference
entry need to b
e modified.

Using Tomcat data sources decouples the database vendor connection
details from the ser
vlet code and gives you the performance benefits gained from using a
connection pool. P
ut simply, always use Tomcat's data sources when accessing a database.

    Caution - When using data sources with both Tomcat 4.1 and Tomcat
4.0, you must pla
ce the JDBC JAR file for your database driver in the
<CATALINA_HOME>/common/lib directo
ry; otherwise, Tomcat will not be able to load the JDBC driver.

hope this helps,
Luke

--
========================
Luke (Terry) Vanderfluit
Mobile: 0421 276 282
========================


pgsql-jdbc by date:

Previous
From: "Mag Gam"
Date:
Subject: JNDI Datasource configuration
Next
From: "Antony Paul"
Date:
Subject: AutoCommit and SQLException