JNDI 问题
我有使用 JNDI 的代码。代码片段如下:
EntityManager createEM(String JNDI ){
EntityManager em = null;
try{
InitialContext ic = new InitialContext();
em = (EntityManager) ic.lookup(JNDI);
return em;
}
catch (Exception ex){
LOG.Error("error in creating em");
ex.printStackTrace();
}
}
现在我收到错误。控件进入 catch 块。
javax.naming.NameNotFoundException: Name comp/env/persistence not found in context "java:".
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1837)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1166)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1095)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(UrlContextImpl.java:1233)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.java.javaURLContextImpl.lookup(javaURLContextImpl.java:394)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:214)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:154)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at javax.naming.InitialContext.lookup(Unknown Source)
我有 2 个项目说 A 和 B。现在项目 B 具有上述方法,并且从项目 A 我从项目 B 调用方法。persist.xml 仅存在于项目 A 中。我是否也需要将 persist.xml 放在项目 A 中?我还得到以下内容作为异常的一部分
javax.naming.NameNotFoundException: Name comp/env/persistence not found in context "java:".
可能是什么原因。我正在使用 websphere。
I have code which uses JNDI.The code snippet as as follows:
EntityManager createEM(String JNDI ){
EntityManager em = null;
try{
InitialContext ic = new InitialContext();
em = (EntityManager) ic.lookup(JNDI);
return em;
}
catch (Exception ex){
LOG.Error("error in creating em");
ex.printStackTrace();
}
}
Now i get error .The control enters catch block.
javax.naming.NameNotFoundException: Name comp/env/persistence not found in context "java:".
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1837)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1166)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1095)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(UrlContextImpl.java:1233)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.java.javaURLContextImpl.lookup(javaURLContextImpl.java:394)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:214)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:154)
[12/28/10 15:51:07:086 GMT+05:30] 00000081 SystemErr R at javax.naming.InitialContext.lookup(Unknown Source)
I have 2 projects say A and B.Now project B has the above method and from project A i am calling mehod from project B.persistent.xml is presnt only in project A.Do i need to place persistent.xml in project A too?I also get the folloing as a part of exception
javax.naming.NameNotFoundException: Name comp/env/persistence not found in context "java:".
What may be the cause.Am using websphere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您对问题的描述有点草率:
我认为您的意思是:persistence.xml 仅存在于项目 B 中。我是否也需要将 persistence.xml 放入项目 A 中?
具体设置是什么?这两个项目是否部署到同一服务器?他们在不同的服务器上吗?项目 A 是否正在访问项目 B 的远程会话 bean?我们在谈论哪些项目? Web 项目、EJB 项目? EAR 项目中同时包含 Web 和 EJB 项目吗?
我认为不支持将实体管理器从一个应用程序发送到另一个应用程序。这个想法是,客户端调用远程 bean,然后该远程 bean 获取本地实体管理器,对其进行一些操作并返回结果。如果 A 是您的客户端,那么 A 当然不需要需要有本地 persistence.xml。
您的问题很可能是您所寻找的实体管理器根本无法以您认为的名称使用。
java:comp/env/persistence
显然不存在。通常实体管理器不会自动出现在 JNDI 中。如果 B 是一个 servlet,您将需要这样的东西:如果 B 是一个 EJB 会话 bean,您需要这样的东西:
此后,实体管理器将在
java:comp/env/persistence
下可用>,但仅当从该 servlet 或 bean 启动 JNDI 查找时。 java:comp/env 是一个相对上下文,对于对其进行查找的每个位置都是不同的。您至少可以做的就是简单地从 B 打印您的
java:comp/env
上下文,并查看其中到底有什么。请参阅这个简单的示例,了解如何进行此类打印: http://tripoverit.blogspot .com/2007/03/print-jndi-tree.htmlYour description of the problem is a little sloppy:
I take it you mean: persistence.xml is present only in project B. Do I need to place persistence.xml in project A too?
What exactly is the setup? Are both projects deployed to the same server? Are they on different servers? Is project A accessing a remote session bean of project B? What projects are we talking about? Web projects, EJB projects? EAR projects with both Web and EJB projects in them?
I don't think it's supported that you ship an entity manager from one application to the other. The idea is that a client calls a remote bean, then this remote bean obtains a local entity manager, does some work with it and returns the results. If A is your client, then A certainly does not need to have a local persistence.xml.
Your problem might well be that the entity manager you're after is simply not available under the name you think it is.
java:comp/env/persistence
obviously doesn't exist. Normally an entity manager doesn't appear automatically in JNDI. If B was a servlet, you would need something like this:If B was an EJB session bean, you need something like this:
After this, the entity manager would be available under
java:comp/env/persistence
, but only when the JNDI lookup is being initiated from that servlet or bean. java:comp/env is a relative context, that is different for each location that a lookup is done on it.The least you could do is simply printing your
java:comp/env
context from B and see what's in there exactly. See this simple example how to do such printing: http://tripoverit.blogspot.com/2007/03/print-jndi-tree.html您是否在 websphere 配置中定义了名为 persistence 的连接?
错误是说你没有定义任何命名的“持久性”
Did u define the connection named persistence in your websphere configuration?
The error is saying u didnt define any named 'persistence'
您可以调试在 websphere 上运行的应用程序吗?这是可以做到的。我建议您在调试模式下运行应用程序并在 jndi 查找点处断点,然后您可以询问 jndi 上下文的状态。
Can you debug the application running on websphere. It is possible to do this. I'd suggest you run the app in debug mode and break point at the jndi lookup point and then you can interrogate the state of the jndi context.