java - JNDI - WAS 7 中的 JDBC 资源查找失败

标签 java jakarta-ee websphere jndi websphere-7

我的代码无法使用 JNDI 查找 JDBC 资源。我收到以下异常:

[根异常是 javax.naming.NameNotFoundException:上下文:ppp-14415Node01Cell/nodes/ppp-14415Node01/servers/server1,名称:jdbc/admincob:未找到名称 admincob 中的第一个组件。 [根异常(exception)是 org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]]

我在 SO 上遵循了这两个解决方案,但仍然无法正常工作

Websphere 6.1 to 7 how to update ibm-web-bnd.xmi to ibm-web-bnd.xml

How do I connect to a Websphere Datasource with a given JNDI name?

这是我的 ibm-web-bnd.xml

    <virtual-host name="default_host" />
<resource-ref name="jdbc/dbcob" binding-name="jdbc/admincob" />

和 web.xml 的一部分

    <resource-ref>
    <description>
Datasource connection to db</description>
    <res-ref-name>jdbc/dbcob</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

绑定(bind)截图如下: binding description

查找代码:

       Context initialContext = new InitialContext();

        DataSource datasource = (DataSource) initialContext
                .lookup("java:comp/env/jdbc/dbcob");
        if (datasource != null) {
            result = datasource.getConnection();
            System.out.println("Data connection retrieved");
            result.close();
        } else {
            System.err.println("Failed to lookup datasource.");
        }

我不确定我错过了什么。请帮忙。

enter image description here enter image description here

最佳答案

发生这种情况是因为您的数据源是在 Cell:/Node:14415Node02/Server:server1 范围内定义的(根据数据源定义的屏幕截图),而您的应用程序配置为运行在 Cell:/Node:14415Node01/Server:server1 上(根据您附加的异常文本)。

换句话说,您正在节点 A 上的服务器 server1 上运行您的应用程序,而数据源定义的范围仅限于节点 B。此数据源只能被范围内的服务器看到在节点 B 内。

关于java - JNDI - WAS 7 中的 JDBC 资源查找失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23812642/

相关文章:

在 jvisulavm 中添加 JMXConnection 时出现 java.lang.SecurityException

java - GWT 模块与 Google App Engine 模块不同,对吧?

java - Spring Boot 与 websphere 配置类不工作

deployment - WebSphere Server不在运行状态:Jython WebSphere脚本?

java - 如何避免 ExecutorService 覆盖 Runnable 的安全主体

java - 如何找到循环内所有测试分数的最高、最低和平均值?

java - 条纹注入(inject)丰富器 : can't connect to ejb module

java - 在 PUB/SUB 模型中消费来自 RabbitMQ 的消息

java - Eclipse 的 Portlet 向导

java - 如何使用 java 流对子列表中的元素进行分组和计数