java - 使用 postgres 池简单连接到数据源

标签 java postgresql wildfly

首先,我不是java程序员,我是系统工程师,我正在尝试在wildfly 9上成功设置postgres连接池。

我这样定义数据源:

    ./jboss-cli.sh

    [disconnected /] connect

    [standalone@localhost:9990 /] module add --name=org.postgres --resources=/tmp/postgresql-9.4-1202.jdbc4.jar --dependencies=javax.api,javax.transaction.api

    [standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgres",driver-class-name=org.postgresql.Driver)
    {"outcome" => "success"}

    [standalone@localhost:9990 /] data-source add --jndi-name=java:/PostGreDS --name=PostgrePool --connection-url=jdbc:postgresql://localhost/postgres --driver-name=postgres --user-name=user --password=password

    [standalone@localhost:9990 /] /subsystem=datasources/data-source=PostgrePool/:write-attribute(name=max-pool-size,value=50)
    {"outcome" => "success"}

当我列出我获得的数据源时:

    [standalone@localhost:9990 /] /subsystem=datasources:read-resource
    {
        "outcome" => "success",
        "result" => {
            "data-source" => {
                "ExampleDS" => undefined,
                "PostgrePool" => undefined
            },
            "jdbc-driver" => {
                "h2" => undefined,
                "postgres" => undefined
            },
            "xa-data-source" => undefined
        }

   }

转到http://localhost:9990/console/App.html#profile/datasources我可以看到这个值:

Name: PostgrePool
JNDI: java:/PostGreDS
Is enabled?: true
Statistics enabled?: false
Driver: postgres

当我单击“连接”->“测试连接”时,它可以工作,所以我认为数据源方面一切正常。

然后我创建了一个 WEB-INF/test.jsp,将其压缩到 test.war(没有其他文件,没有 web.xml)并部署。

<%@page  
 import="java.util.*,javax.naming.*,javax.sql.DataSource,java.sql.*"%>  
<%
InitialContext ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup( "java:PostGreDS");
Connection con=ds.getConnection();
System.out.println("Connection successful");
con.close();
%>

当我加载它时,我得到:

javax.servlet.ServletException:javax.naming.NameNotFoundException:java:PostGreDS

我尝试了我能想到的任何其他方式,jdbc/PostGreDSjdbc/PostgrePool 等等。 我确信我在这里做了一些愚蠢的事情,但我无法找到它是什么。 我还尝试在其中定义一个 web.xml:

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
        version="2.4">
      <description>DB Test App</description>
      <resource-ref>
          <description>DB Connection</description>
          <res-ref-name>java:/PostGreDS</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
      </resource-ref>
    </web-app>

但没有运气。 任何想法?非常感谢。

最佳答案

好吧,算了……我忘记了 java:/PostGreDS 中的“/”。 现在可以了。 我是这样使用连接池的吗?

关于java - 使用 postgres 池简单连接到数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32624629/

相关文章:

java - 使用 HttpClient 在 Java 中进行 OAuth2 身份验证

java - Hibernate 查询缓存在外部更新时自动刷新?

java - 创建具有名称的 bean 时出错,应用程序运行失败。 Spring Boot

SQL 过程错误

java - Wildfly 9.0.2 部署应用程序时出错

java - jmock 和mockito 如何创建包装器?

sql - 如何将数组存储在 SQL 表中?

postgresql - 相等值和相反值的 SQL 查询

java - DRools:如何在事实中的任意嵌套列表上使用

ios - 使用 WildFly 迁移到 JDK 10 和 JSF 2.3 后,iOS 和 Mac OSX 上的站点渲染中断