我在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"
当我直接配置数据时它就工作了来源:
<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>
奏效了。