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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23812642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:20:19  来源:igfitidea点击:

JNDI - JDBC resource lookup failes in WAS 7

javajakarta-eewebspherejndiwebsphere-7

提问by NRJ

My code is unable to do lookup a JDBC resource using JNDI. I am getting the following exception:

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

[Root exception is javax.naming.NameNotFoundException: Context: ppp-14415Node01Cell/nodes/ppp-14415Node01/servers/server1, name: jdbc/admincob: First component in name admincob not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]]

[根异常是 javax.naming.NameNotFoundException: Context: ppp-14415Node01Cell/nodes/ppp-14415Node01/servers/server1, name: jdbc/admincob: First component in name admincob not found。[根异常是 org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]]

I have followed these 2 solution on SO, but still its not working

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

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

Websphere 6.1 到 7 如何将 ibm-web-bnd.xmi 更新为 ibm-web-bnd.xml

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

如何使用给定的 JNDI 名称连接到 Websphere 数据源?

Here is my ibm-web-bnd.xml

这是我的 ibm-web-bnd.xml

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

and portion of web.xml

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

Here is the screenshot of the binding: binding description

这是绑定的屏幕截图: 绑定描述

Lookup code:

查找代码:

       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.");
        }

I am not sure what am I missing. Please help.

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

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

采纳答案by Isaac

This happens because your data source is defined within the scope Cell:/Node:14415Node02/Server:server1(as per your screenshot of the Data Source definition), whereas your application is configured to run on Cell:/Node:14415Node01/Server:server1(as per the exception text that you attached).

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

In other words, you're running your application on server server1on node A, whereas the Data Source definition is scoped to node B. This Data Source can only be seen by servers that are scoped within node B.

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

回答by groo

Martin Baumgartner is probably right, try changing your lookup to:

Martin Baumgartner 可能是对的,请尝试将您的查找更改为:

DataSource datasource = (DataSource) initialContext
            .lookup("jdbc/dbcob");

This will probably fix the error

这可能会修复错误

回答by Robert Panzer

Your binding seems to be ok. java:comp/env/jdbc/dab cob is correctly mapped to jdbc/dbcob. The SystemOut.log of the server should show if a DataSource is bound and under which name.

你的绑定似乎没问题。java:comp/env/jdbc/dab cob 正确映射到 jdbc/dbcob。服务器的 SystemOut.log 应显示是否绑定了 DataSource 以及在哪个名称下。

Kind regards Robert

亲切的问候罗伯特

回答by trikelef

Try something like that in your web.xmldescriptor:

在您的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>
    <lookup-name>jdbc/admincob</lookup-name>
</resource-ref>