I've created a JDBC connection pool PostgreSQL inside my domain on Payara 5.2020.4. The ping is successful. That pool is referenced inside a JDBC resource named jdbc/postgres. The server starts perfectly, domain is started. I then created a small demo JSP trying to access the JDBC resource:
<%@ page import="java.io.*,java.util.*,java.sql.*" %>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<sql:query var="result" dataSource="jdbc/postgres">
SELECT datname,application_name,query FROM pg_stat_activity
</sql:query>
<table border="1" width="100%">
<tr>
<th>datname</th>
<th>application_name</th>
<th>query</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td> <c:out value="${row.datname}"/> </td>
<td> <c:out value="${row.application_name}"/> </td>
<td> <c:out value="${row.query}"/> </td>
</tr>
</c:forEach>
</table>
</body>
</html>
That doesn't work, Payara logs:
javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/postgres"
It works when I configure the dataSource directly:
<sql:setDataSource var="connection" driver="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/test" user="test" password="test" />
<sql:query var="result" dataSource="jdbc/postgres">
I also tried to add the resource to the context.xml, but the message stays the same.
What is missing?
One solution is to add a resource reference inside the web.xml of the application:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<resource-ref>
<description>PostgreSQL JDBC connection</description>
<res-ref-name>jdbc/postgres</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
That worked.