英文:
JNDI data source not accessible from within a JSP page on Payara 5.2020.4
问题
我在我的 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: 无法获取连接,DataSource 无效:"java.sql.SQLException: 未找到适用于 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 中,但消息仍然相同。
缺少什么?
英文:
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?
答案1
得分: 0
一个解决方案是在应用程序的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连接</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>
那样做是有效的。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论