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

时间:2014-05-22 16:39:00

标签: java java-ee websphere jndi websphere-7

我的代码无法使用JNDI查找JDBC资源。我得到以下例外:

[root异常是javax.naming.NameNotFoundException:上下文:ppp-14415Node01Cell / nodes / ppp-14415Node01 / servers / server1, name:jdbc / admincob:找不到名称admincob中的第一个组件。 [root例外是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>

以下是绑定的屏幕截图: 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

4 个答案:

答案 0 :(得分:3)

这是因为您的数据源是在Cell:/Node:14415Node02/Server:server1范围内定义的(根据数据源定义的屏幕截图),而您的应用程序配置为在Cell:/Node:14415Node01/Server:server1上运行(根据例外文本)你附上了。)

换句话说,您在节点A上的服务器server1上运行应用程序,而数据源定义的范围限定为节点B.此数据源只能由节点B中作用域的服务器查看

答案 1 :(得分:0)

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

亲切的问候 罗伯特

答案 2 :(得分:0)

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>

答案 3 :(得分:-1)

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

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

这可能会修复错误