简体   繁体   中英

Simple connection to a datasource with postgres pool

first of all I am not a java programmer, I am a system engineer and I am trying to set up succesfully a postgres connection pool on wildfly 9.

I defined the datasource in this way:

    ./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"}

When I list the datasources I obtain:

    [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
        }

   }

Going to http://localhost:9990/console/App.html#profile/datasources I can see this values:

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

And when I click "Connection" -> "Test Connection" it works, so I think on the datasource side is all ok.

Then I created a WEB-INF/test.jsp, zipped it into test.war (no other files, no web.xml) and deployed.

<%@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();
%>

When I load it I get:

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

I tried in any other way I could think about, jdbc/PostGreDS , jdbc/PostgrePool , and many other. I am sure I am doing something stupid here, but I am not able to find what it is. I tried also to define a web.xml with this inside:

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

but no luck. Any idea? Thank you very much.

Ok, forget it... I had forgotten the "/" in java:/PostGreDS . Now it works. Am I using the connection pool in this way?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM