java NameNotFoundException 将 JNDI 数据源映射到本地名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11532912/
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-10-31 05:28:26  来源:igfitidea点击:

NameNotFoundException mapping JNDI Datasource to Local name

javajndiweblogic12c

提问by Dunderklumpen

Im having trouble mapping a Weblogic (12c) defined JNDI Datasource to a local jndi name.

我无法将 Weblogic (12c) 定义的 JNDI 数据源映射到本地 jndi 名称。

I have a datasource in weblogic mysql datasource defined as "mysqltestds", and I want to map i within a web application to a local name "localds"

我在 weblogic mysql 数据源中有一个数据源定义为“mysqltestds”,我想将 web 应用程序中的 i 映射到本地名称“localds”

My web.xml properties are

我的 web.xml 属性是

<resource-ref>
   <res-ref-name>localds</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>

with the weblogic.xml

使用 weblogic.xml

<weblogic-web-app>
   <resource-description>
      <res-ref-name>localds</res-ref-name>
      <jndi-name>mysqltestds</jndi-name>lls
   </resource-description>
</weblogic-web-app>

When I get the datasource using the global name "mysqltestds" it works correctly.

当我使用全局名称“mysqltestds”获取数据源时,它可以正常工作。

Context initialContext = new InitialContext();
Object resource = initialContext.lookup("mysqltestds");
dataSource = (DataSource) resource; 

However, when I try "localds" it fails with the error:

但是,当我尝试“localds”时,它失败并显示错误:

Context initialContext = new InitialContext();
Object resource = initialContext.lookup("localds");
dataSource = (DataSource) resource; 

javax.naming.NameNotFoundException: Unable to resolve 'localds'. Resolved ''; remaining name 'localds'

javax.naming.NameNotFoundException:无法解析“localds”。解决 ''; 剩余名称“localds”

Can anyone help. I followed the directions in this stackOverflow question Tomcat vs Weblogic JNDI Lookup, but I haven't had any success.

任何人都可以帮忙。我按照这个 stackOverflow 问题Tomcat vs Weblogic JNDI Lookup 中的说明进行操作,但我没有取得任何成功。

采纳答案by Dunderklumpen

I ended up working this out by trial and error.

我最终通过反复试验解决了这个问题。

It seems when you are using a local resource reference, you have to refer to it using the "java:comp/env/.." prefix, so my java code worked when I made the following changes:

似乎当您使用本地资源引用时,您必须使用“java:comp/env/..”前缀来引用它,因此当我进行以下更改时,我的 java 代码可以正常工作:

Context initialContext = new InitialContext();
Object resource = initialContext.lookup("java:comp/env/localds");
dataSource = (DataSource) resource; 

Hope this is useful if anyone else is having similar problems

如果其他人遇到类似问题,希望这是有用的

回答by Shaun Stone

couple things.
first thing to check is make sure you actually deployed the datasource to the server that you are accessing. because if you havent you will get that exact message.

夫妇的事情。
首先要检查的是确保您确实将数据源部署到您正在访问的服务器上。因为如果你没有,你会得到那个确切的消息。

also if you look in the console goto the server page and click the server you are working with.

此外,如果您查看控制台,请转到服务器页面并单击您正在使用的服务器。

then there is a link on the page called something like jndiViewer you should be able to browse for you datasource to confirm its location.

然后页面上有一个名为 jndiViewer 之类的链接,您应该能够浏览数据源以确认其位置。

回答by RAM237

There are really not many topics related to this error, and I have recently faced it when migrating from old Jboss version to WildFly 10.

与此错误相关的主题确实不多,我最近在从旧 Jboss 版本迁移到 WildFly 10 时遇到了它。

I have found out that I can grep through the server.logto discover the correct string to use in lookup.

我发现我可以通过server.loggrep来发现要在lookup.