aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes @Resource injection problem with JNDI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "@Resource injection problem with JNDI" Watch "@Resource injection problem with JNDI" New topic
Author

@Resource injection problem with JNDI

Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
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

Joined: Jul 20, 2005
Posts: 10052
    
163

@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 ]

[My Blog] [JavaRanch Journal]
Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
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

Joined: Jul 20, 2005
Posts: 10052
    
163

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

Joined: Apr 19, 2005
Posts: 189
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

Joined: Jan 17, 2005
Posts: 117
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

Joined: Jul 20, 2005
Posts: 10052
    
163

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @Resource injection problem with JNDI