【问题标题】:java web app and Postgres on websphere-libertywebsphere-liberty 上的 java web 应用程序和 Postgres
【发布时间】:2016-11-23 09:46:30
【问题描述】:

我正在 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>

【问题讨论】:

  • 看起来您的数据源可能在 server.xml 中配置不正确。请将相关部分或完整的 server.xml 添加到问题中。
  • 尝试添加到dataSource type="javax.sql.XADataSource"jdbcDriver javax.sql.XADataSource="org.postgresql.xa.PGXADataSource"
  • 啊,是的,我相信这就是@Gas 的解决方案——您应该将其发布为答案
  • @GAS -- 我尝试将 server.xml 添加到 dataSource 和 jdbcDriver 中,GAS 说什么,但错误是一样的。 WebSphere:“javax.naming.NamingException: CWNEN1001E: java:comp/env/jdbc/postgres JNDI 名称引用的对象无法实例化...” 我真的认为服务器无法获取我请求的类型的对象,因为它没有找到包或包中的类。我可以没有设置类路径吗?我怎样才能检查这个?
  • 你确定这个fileset dir="/arturial/project/ 路径在你的docker容器中是正确的吗?您是否尝试在没有 docker 的情况下运行 Liberty?

标签: java postgresql tomcat docker websphere-liberty


【解决方案1】:

试试这个;

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

另外,a different way

【讨论】:

  • 我试过这个,但没有任何改变。尝试链接中提出的解决方案(我不知道为什么)我的 defaultServer 出错了,并且 advinCenter 无法访问。 Heeeelp
  • 您的 web.xml 是否在正确的目录中? Application.war/WEB-INF/web.xml
  • 是的。相同的应用程序在 tomcat 8 上正确运行
  • 你能用 server.xml 配置更新问题吗?
【解决方案2】:

您的&lt;datasource&gt; 必须在其下配置属性,否则 Liberty 无法知道如何连接到您的数据库。

对于postgresql,试试下面的配置:

<dataSource id="jdbc-Prima_WA_db" jndiName="jdbc/postgres">
  <jdbcDriver libraryRef="postgres-lib"/>
  <properties serverName="172.17.0.3" portNumber="5432" databaseName="Prima_WA_db"  
              user="postgres" password="postgres"/>
</dataSource>

当然,使用与您正在运行的 postgresql 实例相对应的正确值。

【讨论】:

  • @arguibert 你的定义似乎是正确的,但我仍然得到 NamingException: object referenced by java:comp/env/jdbc/postgres JNDI name could not be instatiated
  • 让我们从等式中消除资源引用。您可以直接查找数据源吗?例如:new InitialContext().lookup("jdbc/postgres");
  • 没关系,我在对另一个问题的评论中看到您已经尝试过直接查找。所以问题在于如何在 server.xml 中定义数据源。检查您的服务器日志中是否存在任何指示实例化数据源失败的错误或警告。
  • 在哪里可以找到服务器日志?这是我在 /logs 中找到的 message.log 吗?
  • 日志将在${server.config.dir}/logs 目录中
【解决方案3】:

您的数据源定义可能不完整,请尝试使用以下内容进行更新:

<dataSource type="javax.sql.XADataSource" ...
    <jdbcDriver javax.sql.XADataSource="org.postgresql.xa.PGXADataSource" ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多