【发布时间】:2014-05-22 16:38:59
【问题描述】:
我的代码无法使用 JNDI 查找 JDBC 资源。我收到以下异常:
[根异常是 javax.naming.NameNotFoundException:上下文:ppp-14415Node01Cell/nodes/ppp-14415Node01/servers/server1,名称:jdbc/admincob:未找到名称 admincob 中的第一个组件。 [根例外是 org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]]
我在 SO 上遵循了这 2 个解决方案,但仍然无法正常工作
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>
这是绑定的截图:
查找代码:
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.");
}
我不确定我错过了什么。请帮忙。
【问题讨论】:
-
您的屏幕截图都不能证明确实存在一个与名称
jdbc/admincob绑定的数据源。 -
在 WAS 8 上,对于我来说,查找仅在没有“java:comp/env/”的情况下才有效。
-
我尝试过使用 initialContext .lookup("jdbc/dbcob");还是不行。
-
@Isaac - 更新数据源截图
标签: java jakarta-ee websphere jndi websphere-7