问题
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.
回答1:
Your description of the problem is a little sloppy:
persistent.xml is presnt only in project A.Do i need to place persistent.xml in project A too?
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:
<persistence-context-ref>
<persistence-context-ref-name>persistence</persistence-context-ref-name>
<persistence-unit-name>yourpu</persistence-unit-name>
</persistence-context-ref>
If B was an EJB session bean, you need something like this:
@Stateless
@PersistenceUnit(name="persistence", unitName="yourpu")
public class BBean implements BRemoteInterface {
// some code here...
}
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
回答2:
Did u define the connection named persistence in your websphere configuration?
The error is saying u didnt define any named 'persistence'
回答3:
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.
来源:https://stackoverflow.com/questions/4545593/problem-with-jndi