websphere-liberty 上的 java web 应用程序和 Postgres

标签 java postgresql tomcat docker websphere-liberty

我正在 docker 上的 websphere-liberty 上部署一个工作的 Tomcat webapp。 webapp 连接到 docker 上的 postgres 数据源。在 websphere 中,当我尝试与

建立联系时
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/postgres");
Connection conn = ds.getConnection();

我的 web.xml 设置为:

<resource-ref>
        <description>postgreSQL Connection</description>
        <res-ref-name>jdbc/postgres</res-ref-name>
        <res-type>javax.sql.XADataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

出现以下错误

javax.naming.NamingException: CWNEN1001E: The object referenced by the java:comp/env/jdbc/postgres JNDI name could not be instantiated.
If the reference name maps to a JNDI name in the deployment descriptor bindings for the application
performing the JNDI lookup, make sure that the JNDI name mapping in the deployment descriptor binding is correct.
If the JNDI name mapping is correct, make sure the target resource
can be resolved with the specified name relative to the default initial context.
[Root exception is com.ibm.wsspi.injectionengine.InjectionException: CWNEN0030E: The
server was unable to obtain an object instance for the java:comp/env/jdbc/postgres reference.
The exception message was: CWNEN1004E: The server was unable to find the jdbc/postgres default binding with the javax.sql.XADataSource type for
the java:comp/env/jdbc/postgres reference.]

我排除了 docker 网络的问题。我设置错了什么?我的 websphere-liberty 中有什么东西吗?

server.xml 是

<server description="Default server">

  <!-- Enable features -->
 <featureManager>
  <feature>webProfile-7.0</feature>
  <feature>adminCenter-1.0</feature>
  <feature>jdbc-4.0</feature>
</featureManager>  
  <quickStartSecurity userName="admin" userPassword="password"/>
<!-- Define the host name for use by the collective.
    If the host name needs to be changed, the server should be
    removed from the collective and re-joined. -->
<!--  <variable name="defaultHostName" value="localhost" /> -->

<!-- Define an Administrator and non-Administrator -->
<basicRegistry id="basic">
  <user name="admin" password="admin" />
  <user name="nonadmin" password="nonadminpwd" />
</basicRegistry>

<!-- Assign 'admin' to Administrator -->
<administrator-role>
  <user>admin</user>
</administrator-role>

<!--  <keyStore id="defaultKeyStore" password="Liberty" /> -->

<httpEndpoint id="defaultHttpEndpoint"
             host="*"
             httpPort="9080"
             httpsPort="9443" />

<remoteFileAccess>
  <writeDir>${server.config.dir}</writeDir>
</remoteFileAccess> 
<library id="postgres-lib">
  <fileset dir="/arturial/project/" includes="postgresql-9.4.1208.jre6.jar"/>
</library>
<dataSource id="jdbc-Prima_WA_db" jndiName="jdbc/postgres" type="javax.sql.DataSource">
  <jdbcDriver libraryRef="postgres-lib"/>
  <connectionManager numConnectionsPerThreadLocal="10" id="connectionManager" minPoolSize="1"/>
 <!--   <properties.oracle user="postgres" password="postgres" -
               url="jdbc:postgres://172.17.0.3:5432/Prima_WA_db"/> -->
 </dataSource>
<!--
<applicationManager updateTrigger="disabled"/>
<application id="primawebapp" name="primawebapp" location="war/primawebapp" type="war">
  <classLoader delegation="parentLast" commonLibraryRef="postgres-lib"/>
 </application>
-->
</server>

最佳答案

试试这个;

DataSource ds = (DataSource)ctx.lookup("jdbc/postgres");

此外,a different way .

关于websphere-liberty 上的 java web 应用程序和 Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38476467/

相关文章:

java - 将域名指向VPS中的java应用程序

java - 将大流复制到字符串 - Java

java - 如何在 Joda 中使用 JDK GregorianCalendar 对象日期

postgresql jsonb 在对象中查找键

search - 使用 tomcat 在 solr 中再次触发相同查询时日志中的差异

html - Tomcat7 -> Tomcat8 - 服务器只打印一半的 html 页面

java - 在 JAVA 中使用 Apache POI 和 iText 从 Word (DOC) 创建 PDF

Java创建编译类型参数未知的泛型对象

python - 提交后保持对数据库对象的锁定

ruby-on-rails - Rails 4.2.0 中简单整数赋值的 RangeError 应该被验证捕获