You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Robin Wierenga <Ro...@backstream.com> on 2003/01/28 13:09:54 UTC

Tomcat 4.1.18 Anyone know how to define global JNDI resource

Hi !

I want to define some sort of global JNDI resource in my server.xml
configuration file. I know setting the crossContext="true" will enable
sharing but has certain security implications which I'm unaware of.

A. Is this possible
B. How to do it ?

Thanks,
Robin

- Tomcat newbie


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Tomcat 4.1.18 Anyone know how to define global JNDI resource

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 28 Jan 2003, Robin Wierenga wrote:

> Date: Tue, 28 Jan 2003 13:09:54 +0100
> From: Robin Wierenga <Ro...@backstream.com>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: tomcat-user@jakarta.apache.org
> Subject: Tomcat 4.1.18 Anyone know how to define global JNDI resource
>
> Hi !
>
> I want to define some sort of global JNDI resource in my server.xml
> configuration file. I know setting the crossContext="true" will enable
> sharing but has certain security implications which I'm unaware of.
>

Note that "crossContext" is not relevant for sharing JNDI resources.

> A. Is this possible

Yes.

> B. How to do it ?

(1) Define a resource (such as a database connection pool) in the
    <GlobalNamingResources> section of server.xml:

    <GlobalNamingResources>
      ...
      <Resource name="MyDatabase" auth="Container"
                type="javax.sql.DataSource"
         description="Shared database connection pool"/>
      <ResourceParams name="MyDatabase">
        ... <parameter> elements to configure your connection pool ...
      </ResourceParams>
      ...
    </GlobalNamingResources>

(2) Define a resource link to each shared resource in the <Context>
    element for an application:

    <Context path="/myapp" ...>
      ...
      <ResourceLink name="jdbc/Customers" global="MyDatabase"/>
      ...
    </Context>

(3) In your web.xml, declare a resource reference:

    <resource-ref>
      <description>
        Symbolic reference to the customer database.  Note that the
        web application has no idea whether the actual connection pool
        is shared or not.
      </description>
      <res-ref-name>jdbc/Customers</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref>

(4) In your application, use the connection pool like this:

    InitialContext ic = new InitialContext();
    Context env = (Context) ic.lookup("java:comp/env");
    DataSource ds = (DataSource) env.lookup("jdbc/Customers");
    Connection conn = null;
    try {
      conn = ds.getConnection();
      ... use the connection for a while ...
    } catch (SQLException e) {
      ... deal with database error ...
    } finally {
      conn.close(); // Returns connection to the pool
    }

You can repeat steps (2)-(4) for as many different apps as you like.  As
long as the "global" attribute is the same, the underlying global resource
will be shared.

If you later decided that a particular app needed its own connection pool
(to improve performance or something), all you would need to do is replace
the <ResourceLink> statement for that app with a
<Resource>/<ResourceParams> pair that defined the resource nested inside
the <Context> element.  The application cannot tell the difference.

Further info on JNDI resources is available in the Tomcat docs that are
part of a standard installation, or available online:

  http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-resources-howto.html

>
> Thanks,
> Robin
>

Craig




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>