Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Resource injection problem with JNDI

 
Sreedevi Vinod
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.

 
Jaikiran Pai
Marshal
Pie
Posts: 10444
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
Marshal
Pie
Posts: 10444
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.
 
vu lee
Ranch Hand
Posts: 206
  • 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
Marshal
Pie
Posts: 10444
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic