Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JNDI local context

 
Anant Kabra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm having a problem with JNDI lookups on the local JNDI Context (java:comp/env) in my application.
Ok here is the problem
A method is called in a class. I print out the java:comp/env context bindings in this method, and it shows me all the objects that I expect to be bound to java:comp/env context. Then it calls a method in another class. I again print out the java:comp/env bindings in this method. Here there are no bindings in java:comp/env.
Any ideas why this might happen?
Thanks
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anant,
I think, for example, if your SessionBean class A tries and successfully lookup a JDNI context, and then calls methods located in other classes, this lookup will NOT be carry forward to that class, you need to look it up again in that class.
If you want the class notifies the JNDI context, you can pass the context to that class, and see whether it is bind.
Nick.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bindings are carried along with the thread. If you spin off another thread it could cause this to happen.
Kyle
 
Anant Kabra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok but the resources are defined in the web.xml file as <resource-ref>s so I would think that these would be available to all threads?
Also how does this work with EJBs? If the resource is defined in the ejb-jar.xml file, is this resource available to the ejb's as well as the methods on other classes that the ejb invokes?
For example lets take the following scenario.
Res1 -> JMS Queue
EJB1 -> defines Res1 in ejb-jar.xml
IntClass1 -> class contained in EJB1-ejb.jar
ExtLib1 -> contains ExtClass1
EJB2 -> another EJB that does not have Res1 defined
EJB1 calles method on IntClass1.. is Res1 available to method?
EJB1 calles method on ExtClass1.. is Res1 available to method?
EJB1 looks up EJB2 and calles method on EJB2.. is Res1 available to method?
Thanks,
-- Anant
 
Gary Craig
Author
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please indicate how you are trying to access local reference in each method. My assumption is you are doing a lookup(java:comp/env/...) in each case. Are you "building" an Initial context in each case or are you reusing a common Context?
It is the Context that either has a reference bound (at runtime) or not.
 
Anant Kabra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using java:comp/env to do the lookup
Also I'm building the initial context each time.
Thanks,
-- Anant
 
Daniel Berg
author
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be wise to cache the InitialContext within the Application (you could use a utility JAR service locator approach). This would allow you to use the same context for all clients and it will improve performance because you are not constantly creating the same InitialContext.
Dan
Co-Author of "Enterprise Java (tm) Programming with IBM Websphere, 2nd Edition".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic