在Payara 5.2020.4的JSP页面中无法访问JNDI数据源

问题描述

我在Payara 5.2020.4上的域内创建了JDBC连接池Postgresql。 ping成功。该池在名为jdbc / postgres的JDBC资源中引用。服务器完美启动,域启动。 然后,我创建了一个小型演示JSP,尝试访问JDBC资源:

<%@ 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>

那行不通,Payara记录:

javax.servlet.servletexception: javax.servlet.jsp.JspException: Unable to get connection,DataSource invalid: "java.sql.sqlException: No suitable driver found for jdbc/postgres"

当我直接配置dataSource时它起作用:

<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">

我还试图将资源添加到context.xml中,但是消息保持不变。

缺少什么?

解决方法

一种解决方案是在应用程序的web.xml内添加资源引用:

<?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>

那行得通。