• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

@Resource injection problem with JNDI

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I want to do a JNDI lookup of a data source using the @Resource annotation. When I use the string "java:comp/env/demoDS" or "demoDS" it does not work. But if I use the lookup() method of the SessionContext and pass the name "demoDS", it works perfectly. Why doesn't the JNDI lookup work? The code is shown below.

Thanks,
Devi.

 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

@Stateless
@Resource(name="demoDS",mappedName="java:/demoDS")
@RemoteBinding(jndiBinding="ProcessRemote")
public class ProcessPaymentBean implements ProcessPayment



Shouldn't the resource injection be at field level instead of class level? Something like:



Have a look at JBoss EJB3 Trailblazer - Injection example
[ April 18, 2007: Message edited by: Jaikiran Pai ]
 
Sreedevi Vinod
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read that resource injection works at both class level and field level. If the same resource is required in multiple fields, we might choose class level injection. So why isn't it working for me when I use a JNDI lookup? Like I said, it does work when I use the lookup() method of SessionContext.

Thanks,
Devi
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So why isn't it working for me when I use a JNDI lookup? Like I said, it does work when I use the lookup() method of SessionContext



Sorry, misunderstood your question in the first post.

While looking up through InitialContext did you try:



If this doesnt work, post the contents of your -ds.xml file.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do what Jaikiran Pai suggested. If it works, it meant some how your resource was not declared under java name space. In that case, remove java name space from annotation

@Resource(name="demoDS",mappedName="demoDS")

Otherwise, you can loop through and print whatever bound to the initial context to detect the JNDI name for your datasource.
 
Sreedevi Vinod
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Jai Kiran and Vu Lee.
When I changed the code as below, it works.

Can you explain why this JNDI name is not bound to the java:comp/env namespace? What do I need to do to bind the JNDI name of the data source to java:comp/env?
Also, what is the code that I can use to loop and print out all the JNDI bindings?

Thanks,
Devi
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you explain why this JNDI name is not bound to the java:comp/env namespace?



By default, JBoss binds your datasource to the java: namespace in the JNDI.

What do I need to do to bind the JNDI name of the data source to java:comp/env?



You can create a resource-ref entry for your application in the ejb-jar.xml and jboss.xml as follows:

Your ejb-jar.xml:



Your jboss.xml:



Then in your code you can access this datasouce using the java:comp/env/demoDS JNDI name.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic