<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>

This page talks about how to use the database pool ${pool.name} from a J2EE application. The example here is a web application, but other application modules would work in the same way.

WEB-INF/web.xml

The web.xml should have a resource-ref section declaring the database pool, like this. Note the res-ref-name, which is what we'll need to map the reference to a pool, and also what the application will need in order to access the pool.

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

  <!--  servlets and mappings and normal web.xml stuff here -->

  <resource-ref>
    <res-ref-name>jdbc/MyDataSource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>
</web-app>

WEB-INF/geronimo-web.xml

To point the resource reference to a specific database pool in Gernimo, the web application needs to have a geronimo-web.xml deployment plan. That may be packaged in the WAR in the WEB-INF directory, or it may be provided separately on the command line to the deploy tool. The geronimo-web.xml plan should have a dependency element pointing to the database pool module, and a resource-ref block corresponding to the web.xml resource-ref above, which maps the resource reference to a specific database pool. In that block, the ref-name must match the res-ref-name from the web.xml (above) and the resource-link must point to the database pool by name.

If you have only one pool named ${pool.name} deployed in Geronimo, you can point to it like this.

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1">
    <environment>
        <moduleId>
            <artifactId>MyWebApp</artifactId>
        </moduleId>
        <dependencies>
            <dependency>
                <groupId>${pool.abstractNameMap['groupId']}</groupId>
                <artifactId>${pool.abstractNameMap['artifactId']}</artifactId>
            </dependency>
        </dependencies>
    </environment>

    <context-root>/MyWebApp</context-root>

    <!-- security settings, if any, go here -->

    <resource-ref>
        <ref-name>jdbc/MyDataSource</ref-name>
        <resource-link>${pool.name}</resource-link>
    </resource-ref>
</web-app>

That will search for a pool named ${pool.name} in the current application and any modules listed as dependencies (and their dependencies, etc.).

If you have more than one pool named ${pool.name} (for example, two dependencies that each include a component named ${pool.name}), then you can specify the pool to use more explicitly like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1">
    <environment>
        <moduleId>
            <artifactId>MyWebApp</artifactId>
        </moduleId>
        <dependencies>
            <dependency>
                <groupId>${pool.abstractNameMap['groupId']}</groupId>
                <artifactId>${pool.abstractNameMap['artifactId']}</artifactId>
            </dependency>
        </dependencies>
    </environment>

    <context-root>/MyWebApp</context-root>

    <!-- security settings, if any, go here -->

    <resource-ref>
        <ref-name>jdbc/MyDataSource</ref-name>
        <pattern>
          <groupId>${pool.abstractNameMap['groupId']}</groupId>
          <artifactId>${pool.abstractNameMap['artifactId']}</artifactId>
          <name>${pool.abstractNameMap['name']}</name>
        </pattern>
    </resource-ref>
</web-app>

Application Code

To get a reference to the database pool, your application can use code like this. Note that the JNDI lookup string is java:comp/env/ plus the res-ref-name used in web.xml (above).

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    try {
        InitialContext ctx = new InitialContext();
        DataSource ds = ctx.lookup("java:comp/env/jdbc/MyDataSource");
        Connection con = ds.getConnection();
    } catch(NamingException e) {
        ...
    } catch(SQLException e) {
        ...
    }
}

">Return to list