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

huangapple go评论65阅读模式
英文:

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:

&lt;%@ page import=&quot;java.io.*,java.util.*,java.sql.*&quot; %&gt;
&lt;%@ page import=&quot;javax.servlet.http.*,javax.servlet.*&quot; %&gt;
&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot; %&gt;
&lt;%@ taglib prefix=&quot;sql&quot; uri=&quot;http://java.sun.com/jsp/jstl/sql&quot; %&gt;
&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;sql:query var=&quot;result&quot; dataSource=&quot;jdbc/postgres&quot;&gt;
    SELECT datname,application_name,query FROM pg_stat_activity
&lt;/sql:query&gt;
&lt;table border=&quot;1&quot; width=&quot;100%&quot;&gt;
    &lt;tr&gt;
        &lt;th&gt;datname&lt;/th&gt;
        &lt;th&gt;application_name&lt;/th&gt;
        &lt;th&gt;query&lt;/th&gt;
    &lt;/tr&gt;
    &lt;c:forEach var=&quot;row&quot; items=&quot;${result.rows}&quot;&gt;
        &lt;tr&gt;
            &lt;td&gt; &lt;c:out value=&quot;${row.datname}&quot;/&gt; &lt;/td&gt;
            &lt;td&gt; &lt;c:out value=&quot;${row.application_name}&quot;/&gt; &lt;/td&gt;
            &lt;td&gt; &lt;c:out value=&quot;${row.query}&quot;/&gt; &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/c:forEach&gt;
 &lt;/table&gt;
 &lt;/body&gt;
 &lt;/html&gt;

That doesn't work, Payara logs:

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: &quot;java.sql.SQLException: No suitable driver found for jdbc/postgres&quot;

It works when I configure the dataSource directly:

&lt;sql:setDataSource var=&quot;connection&quot; driver=&quot;org.postgresql.Driver&quot; url=&quot;jdbc:postgresql://localhost:5432/test&quot; user=&quot;test&quot; password=&quot;test&quot; /&gt;
&lt;sql:query var=&quot;result&quot; dataSource=&quot;jdbc/postgres&quot;&gt;

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    	 xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
	 xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;
	 version=&quot;4.0&quot;&gt;
    &lt;resource-ref&gt;
	    &lt;description&gt;PostgreSQL JDBC connection&lt;/description&gt;
	    &lt;res-ref-name&gt;jdbc/postgres&lt;/res-ref-name&gt;
	    &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
	    &lt;res-auth&gt;Container&lt;/res-auth&gt;
     &lt;/resource-ref&gt;
&lt;/web-app&gt;

That worked.

huangapple
  • 本文由 发表于 2020年9月9日 17:40:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63808945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定