Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

@Resource injection problem with JNDI

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic