%@ 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 resource-ref block corresponding to the one 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.
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.0" xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.0" configId="MyConfigName"> <context-root>/MyWebApp</context-root> <context-priority-classloader>true</context-priority-classloader> <!-- security settings, if any, go here --> <naming:resource-ref> <naming:ref-name>jdbc/MyDataSource</naming:ref-name> <naming:resource-link>${pool.name}</naming:resource-link> </naming:resource-ref> </web-app>
That will search for a pool named ${pool.name} in both the current application and as a server-wide pool.
If you have more than one pool named ${pool.name} (for example, one as a server-wide pool and one deployed within an application EAR), 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.0" xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.0" configId="MyConfigName"> <context-root>/MyWebApp</context-root> <context-priority-classloader>true</context-priority-classloader> <!-- security settings, if any, go here --> <naming:resource-ref> <naming:ref-name>jdbc/MyDataSource</naming:ref-name> <naming:domain>${pool.objectNameMap['domain']}</naming:domain> <naming:server>${pool.objectNameMap['J2EEServer']}</naming:server> <naming:application>${pool.objectNameMap['J2EEApplication']}</naming:application> <naming:module>${pool.objectNameMap['JCAResource']}</naming:module> <naming:type>${pool.objectNameMap['j2eeType']}</naming:type> <naming:name>${pool.objectNameMap['name']}</naming:name> </naming: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) { ... } }