Skip to main content

JNDI DataSource Lookups and Spring

4 replies [Last post]
jdlee
Offline
Joined: 2006-01-25

I'm trying to deploy an app (EJB3) to Glassfish, and I'm having trouble with the JNDI lookup, so I'm hoping someone here can help me (I cross-posted this question to the Spring forum).

In the constructor for my stateless session bean, I grab the Spring context, then retrieve the JdbcTemplate and set that on my bean (which extends JdbcDaoSupport). Here's my applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

java:comp/env/jdbc/ItemMap

Some notes on the definition for brandNameProductDataSource. The bean deploys and runs great un JBoss 4 if I uncomment the resourceRef line, and change the JNDI name to java:jdbc/ItemMap. On Glassfish, however, I get errors like this:

Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'brandNameProductDataSource' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is javax.naming.NamingException: invocation exception

For the JNDI name, I've tried every combination I can think of

* jdbc/ItemMap
* java:jdbc/ItemMap
* java:comp/env/jdbc/ItemMap

All to no avail. Has anyone run across a similar scenario and had any luck? Thanks!

jason

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ss141213
Offline
Joined: 2005-03-30

I don't know Spring. So my reply is based on Java EE knowledge. For you to be able to do a JNDI lookup of "java:comp/env/jdbc/ItemMap", you need to bind this name to to something in the JNDI tree. Are you doing it any where? I guess, this name is used to reference a resource of some kind in your session bean. In that case, either you should do:
@Resource(name="jdbc/ItemMap") in your code
or use

jdbc/ItemMap
...

in your ejb-jar.xml.

Then you need to bind this logical name to an actual object using either sun-ejb-jar.xml or mapped-name (https://glassfish.dev.java.net/nonav/javaee5/api/s1as-javadocs/javax/annotation/Resource.html#mappedName()).

-- Sahoo

jdlee
Offline
Joined: 2006-01-25

I do have the datasource and the connection pool mapped in the server. From the admin console, I can ping the connection pool successfully, and I have been able to retrieve the DS via Spring and JNDI from a web, but there's something not getting done when using Spring and JNDI from my sessions bean. I worked around the issue by using container injection for the DS, but I'd like to know how to use Spring to do that, as container injection might not always be a viable option.

jason

ss141213
Offline
Joined: 2005-03-30

Even though you have set up datasource and connection pool, is there still not needed to define a [b]resource reference[/b] in the java:comp/env subcontext of JNDI? Can you post the code snippet that gets the NamingException? I want to see how it is looking up. Is this look up happenning Spring code?

Thanks,
Sahoo

jdlee
Offline
Joined: 2006-01-25

As to the need for the resource reference, I don't know. I'm fairly new to EJB(3). It works fine from the web app deployed to the same app server, so I assumed ;) that it should work fine from the session bean.

The look up [b]is[/b] happening in Spring code, so I can't (easily) post where that's happening.