Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ejb jndi  RSS feed

 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why there are two ways of finding objects using JNDI?
Anyone could tell me the difference between these?
1.
javax.naming.Context context=new javax.naming.InitialContext();
Object obhect=context.loopup("java:comp/env/ejb/OrderItem");
2.
String url="t3://localhost:7001";
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
Context ctx=new InitialContext(h);
Object ds = ctx.lookup("SQLServer");
Thanx in advance!
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between the lookup call in your examples is that the first one uses the current EJB's private context for looking up an EJB reference while the second one uses the real JNDI name of the object being looked up.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost forgot... Your examples also demonstrate two different ways of getting a reference to the JNDI context (the first is used to get the "local" JNDI context from within EJB or webapp code, the second one is used to get a remote JNDI context).
 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,can you tell me what's an EJB's private Context?
Under what circumtances can a session bean loop up for entity beans through its private context?
I just want to figure out when should i use a bean's private context and when to use real jndi name?
thanx in advance!
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't heard something called private and real JNDI context. Can some enlighten me.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,can you tell me what's an EJB's private Context?
Under what circumtances can a session bean loop up for entity beans through its private context?
I just want to figure out when should i use a bean's private context and when to use real jndi name?

The "private context" I was referring to is the JNDI context under "java:comp/env". That's where all the per-EJB resources are bound, such as ejb-ref, resource-ref, UserTransaction, and so on. It's basically a namespace protecting you from JNDI name clashes.
For when to use the private context, I'd say as often as you can. It doesn't cost you anything besides the extra work you need to do on defining the references in your deployment descriptors (ejb-jar.xml and vendor specific dd such as weblogic-ejb-jar.xml).
 
norman richards
Author
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Bhat:
I haven't heard something called private and real JNDI context. Can some enlighten me.

In order to find another bean, you need to look it up by it's name in the JNDI context. This means that in your code, you need to essentially hardcode the link from one bean to another. However, EJB is supposed to be a component
model where you can, at deployment time, change the linkings between beans or substitute one implementation for another.
The solution is to use an alias. This is what the others have refered to as "private" or "local". Basically, in your code, you can ask for a "foo" and in your deployment descriptors you can say which physical bean you meant when you asked for "foo".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!